#include<iostream> #include<vector> #define ll long long #define PLL pair<ll,ll> #define pb push_back usingnamespace std; constint N = 3e5 + 5; int n; int w[N];//存每个点快乐指数 vector<PLL > tree[N]; ll dp[N]; ll ans = 0; voiddfs(int u, int father){ ans = max(ans, dp[u]); for (int i = 0; i < tree[u].size(); i++) { int son = tree[u][i].first; if (son == father)continue; dfs(son, u); ans = max(ans , dp[son] + dp[u] - tree[u][i].second);//dp[son] + dp[u] - tree[u][i].second:倒v的形状,最大值+次大值 dp[u] = max(dp[u], dp[son] + w[u] - tree[u][i].second); ans = max(ans, dp[u]); } } intmain(){ cin >> n; for (int i = 1; i <= n; i++)cin >> w[i]; for (int i = 1; i <= n; i++)dp[i] = w[i], ans = max(ans, dp[i]); for (int i = 0; i < n - 1; i++) { ll a, b, c; cin >> a >> b >> c; tree[b].pb({a, c}); tree[a].pb({b, c}); } dfs(1, 0); for (int i = 1; i <= n; i++)ans = max(ans, dp[i]); cout << ans << '\n'; }
int component = 1;//连通分量数量 ll kruskal(){ sort(edges, edges + m);
for (int i = 1; i <= n; i++) { p[i] = i; path[i].push_back(i); } // 初始化并查集和路径
ll res = 0, cnt = 0; for (int i = 0; i < m; i++) { int a = edges[i].a, b = edges[i].b, w = edges[i].w; a = find(a), b = find(b); if (a != b)//找到的祖宗不相等说明不连通 { p[a] = b; edges[i].vis = true;//标记进去最小生成树的集合 res += w; cnt++; for (auto j:path[a]) for (auto k:path[b]) { d[j][k] = d[k][j] = w; //d[i][j]代表i到j点的路径上最大的一条边(之前排过序,w是递增的) } for (auto j:path[a]) path[b].push_back(j); //合并路径,标记a能到b }
}
for (int i = 1; i <= n - 1; i++) { if (find(i) != find(i + 1)) { component++; } } if (component > 1)return-1; return res; }
intmain(){ ll sum = 0; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int a, b, w; scanf("%d%d%d", &a, &b, &w); edges[i] = {a, b, w}; sum += w; }
int ans = kruskal(); int res=INF; for (int i = 0; i < m; i++) if (!edges[i].vis) //枚举每条没加入最小生成树的集合 res = min(res, ans + edges[i].w - d[edges[i].a][edges[i].b]); if (ans == -1)cout << "No MST\n" << component; else{ if(res>ans)cout<<ans<<'\n'<<"Yes"; else{ cout<<ans<<'\n'<<"No"; } }
ll res = 0, cnt = 0; for (int i = 0; i < m; i++) { int a = edges[i].a, b = edges[i].b, w = edges[i].w;
a = find(a), b = find(b); if (a != b)//找到的祖宗不相等说明不连通 { p[a] = b; res += w; cnt++; } else { if (w < 0)res += w; } }
return res; }
intmain(){ ll sum = 0; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int a, b, w; scanf("%d%d%d", &a, &b, &w); edges[i] = {a, b, w}; sum += w; }
int n, m; vector<ll> g[N]; ll dis[N], son_size[N];
voiddfs(ll u, ll fa, ll d){
for (auto i: g[u]) { if (i == fa)continue; dis[1] += d + 1;//根节点到其他所有点的距离之和 dfs(i, u, d + 1); son_size[u] += son_size[i];
} }
voiddfs2(ll u, ll fa){ for (auto i: g[u]) { if (i == fa)continue; dis[i] = dis[u] - (son_size[i] - 1) + (n - son_size[i]-1);//每次从节点移动到其儿子,都可以通过这个节点到其他点的距离之和用该公式推出其儿子节点到其他点的距离之和 dfs2(i, u); } }
intmain(){ //ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++)son_size[i] = 1; for (int i = 1; i <= n - 1; i++) { int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } dfs(1, 1, 0); //cout << dis[1] << '\n'; dfs2(1,1); for (int i = 1; i <= n; i++) { cout << dis[i] << '\n'; }
return0; }
inline ll read(){ char ch = getchar(); ll p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-')p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }
#include<bits/stdc++.h> //#pragma GCC optimize(2) //#pragma GCC optimize("inline") //#pragma GCC optimize("-fgcse") //#pragma GCC target("avx","sse2") //#pragma GCC optimize("-fgcse-lm") //#pragma GCC optimize("-fipa-sra") //#pragma GCC optimize("-ftree-pre") //#pragma GCC optimize("-ftree-vrp") //#pragma GCC optimize("-fpeephole2") //#pragma GCC optimize("-ffast-math") //#pragma GCC optimize("-fsched-spec") //#pragma GCC optimize("unroll-loops") usingnamespace std; #define ll long long #define PII pair<int,int> #define PLL pair<ll,ll> #define PIII pair<int,PII> #define PLLL pair<ll,PLL> #define fi first #define se second #define pb push_back //#define LOCAL #define debug(a) cout << #a << " " << a << '\n'; constint N = 1e6 + 5; constint M = 1e5 + 5; const ll INF = 0x3f3f3f3f3f3f3f3f; const ll mod = 1e9 + 7;
inline ll read();
vector<int> g[N]; int n, m; ll ans;
ll dfs(int u, ll dep){ ll ma = 0; for (auto i: g[u]) { ll t = dfs(i, dep + 1);//叶子结点到当前结点的距离 ans += min(dep, t);//从根结点到当前结点,从叶子结点到当前结点,取一个最小值 ma = max(ma, t);//2.ma遇到分支后将分支的所有链长取最大值,就是该分支下的最长链长 } ans -= min(ma, dep);//减掉最长的链重复走的 return ma + 1;//1.ma没遇到分支时就相当于只是递归的求叶子结点到当前结点的距离 }
voidsolve(){ n = read(); for (int i = 1; i <= n; i++)g[i].clear(); for (int i = 2; i <= n; i++) { int f = read(); g[f].pb(i); } ans = n - 1; dfs(1, 0); cout << ans << '\n'; }
intmain(){ //ios::sync_with_stdio(false); #ifdef LOCAL int begin_time = clock(); freopen("../input.txt", "r", stdin); // freopen("../output.txt", "w", stdout); #endif int T = 1; T = read(); for (int cas = 1; cas <= T; cas++) {
printf("Case #%d: ", cas); solve(); }
#ifdef LOCAL int end_time = clock(); printf("\nRun time: %.2lf ms", (double) (end_time - begin_time) / CLOCKS_PER_SEC * 1000); #endif
return0; }
inline ll read(){ char ch = getchar(); ll p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-')p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }
#pragma GCC optimize(2) usingnamespace std; #define ll long long #define PII pair<int,int> #define PLL pair<ll,ll> #define fi first #define se second #define pb push_back #define debug(a) cout << #a << " " << a << '\n'; constint N = 505; constint M = 1e5 + 5; constint INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; const ll mod = 1e9 + 7;
inline ll read();
int n, m; char g[N][N]; int dis[N][N]; bool st[N][N];
voidbfs(){ deque<PII > dq; memset(dis, 0x3f, sizeof dis); memset(st, 0, sizeof st); dis[0][0] = 0; dq.pb({0, 0}); int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; while (!dq.empty()) { auto t = dq.front(); dq.pop_front(); if (st[t.fi][t.se])continue; st[t.fi][t.se] = true; for (int i = 0; i < 4; i++) { int a = t.fi + dx[i], b = t.se + dy[i]; if (a < 0 || a >= n || b < 0 || b >= m)continue; if (g[a][b] == '#') { for (int j = -1; j <= 1; j++) { for (int k = -1; k <= 1; k++) { int xx = a + j, yy = b + k; if (xx < 0 || xx >= n || yy < 0 || yy >= m)continue; if (dis[t.fi][t.se] + 1 < dis[xx][yy]) { dis[xx][yy] = dis[t.fi][t.se] + 1; dq.push_back({xx, yy}); }
#pragma GCC optimize(2) usingnamespace std; #define ll long long #define PII pair<int,int> #define PLL pair<ll,ll> #define fi first #define se second #define pb push_back #define debug(a) cout << #a << " " << a << '\n'; constint N = 2e5 + 5; constint M = 1e5 + 5; constint INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; const ll mod = 1e9 + 7;
inline ll read();
int dis[N]; bool st[N]; int n, m; vector<int> g[N]; int minn[N];
voiddijkstra(){ priority_queue<PII, vector<PII >, greater<>> heap; memset(dis, 0x3f, sizeof dis); memset(st, false, sizeof st); dis[1] = 0; heap.push({0, 1}); while (!heap.empty()) { auto t = heap.top(); heap.pop(); int ver = t.se, distance = t.fi; if (st[ver])continue; st[ver] = true; for (auto i: g[ver]) { if (dis[i] >= distance + 1) { if (dis[i] == distance + 1)minn[i] =(minn[i]+ minn[t.se])%mod;//记录条数 else { dis[i] = distance + 1; minn[i]=minn[t.se]; //cout<<i<<' '<<minn[i]<<"\n"; heap.push({dis[i], i}); } } } } }
intmain(){ //ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; g[a].pb(b); g[b].pb(a); } minn[1] = 1; dijkstra();
cout << minn[n];
return0; }
inline ll read(){ char ch = getchar(); ll p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-')p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }
mat() { memset(m, 0, sizeof(m)); } }; ll n,k,mod; mat operator*(mat a, mat b) {//定义矩阵乘法 mat ans; ll x = 0; for (int i = 0; i < tot; i++) { for (int j = 0; j < tot; j++) { x = 0; for (int k = 0; k < tot; k++) { x = (x + a.m[i][k] * b.m[k][j])%mod; } ans.m[i][j] = x; } } return ans; }
mat init(mat &a){//初始化单位矩阵(按要求自定义) for (int i = 0; i < tot; i++) { for(int j=0;j<tot;j++){ a.m[i][j]=0; } } for (int i = 0; i < tot; i++) { a.m[i][i] = 1; } return a; }
mat mat_pow(mat a, ll k){//矩阵快速幂 mat ans = init(ans); while (k) { if (k & 1) ans = ans * a; a = a * a; k >>= 1; } return ans; }
//define LOCAL usingnamespace std; #define ll long long #define PII pair<int,int> #define PLL pair<ll,ll> #define PIII pair<int,PII> #define PLLL pair<ll,PLL> #define fi first #define se second #define pb push_back #define debug(a) cout << #a << " " << a << '\n'; constint N = 205; const ll INF = 0x3f3f3f3f3f3f3f3f; const ll mod = 998244353;
inline ll read();
int n, m; int a[N][N]; int origin_a[N][N]; int b[N][N]; ll qpow(ll a,ll b){ ll res=1; while(b){ if(b&1)res=(res*a)%mod; a=(a*a)%mod; b>>=1; } return res; } voidinit(){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ a[i][j]=origin_a[i][j]; } } } intgauss(){ int c, r; for (c = 0, r = 0; c < n; c++) { int t = r; for (int i = r; i < n; i++) if (a[i][c]) t = i;
if (!a[t][c]) continue;
for (int i = c; i < n + 1; i++) swap(a[r][i], a[t][i]); for (int i = r + 1; i < n; i++)//将下面所有行的第c列消成0 if (a[i][c]) for (int j = n; j >= c; j--) a[i][j] ^= a[r][j];//该行第c列肯定是1,所以下面行异或该行第c列肯定可以消成0(两个1异或)
r++; }
if (r < n) { for (int i = r; i < n; i++) if (a[i][n]) return-1; return n-r; }
for (int i = n - 1; i >= 0; i--) for (int j = i + 1; j < n; j++) a[i][n] ^= a[i][j] & a[j][n];
#include<bits/stdc++.h> //#pragma GCC optimize(2) //#pragma GCC optimize("inline") //#pragma GCC optimize("-fgcse") //#pragma GCC target("avx","sse2") //#pragma GCC optimize("-fgcse-lm") //#pragma GCC optimize("-fipa-sra") //#pragma GCC optimize("-ftree-pre") //#pragma GCC optimize("-ftree-vrp") //#pragma GCC optimize("-fpeephole2") //#pragma GCC optimize("-ffast-math") //#pragma GCC optimize("-fsched-spec") //#pragma GCC optimize("unroll-loops") usingnamespace std; #define ll long long #define PII pair<int,int> #define PLL pair<ll,ll> #define fi first #define se second #define pb push_back #define debug(a) cout << #a << " " << a << '\n'; constint INF = 0x3f3f3f3f; const ll LLINF =0x3f3f3f3f3f3f3f3f; const ll mod = 1e9 + 7; inline ll read(); int n; double origin_a[15][15]; double a[15][15]; constdouble eps=1e-8; intgauss() { int c, r; for (c = 0, r = 0; c < n; c ++ )//枚举每一列 { int t = r; for (int i = r; i < n; i ++ ) if (fabs(a[i][c]) > fabs(a[t][c]))//找到绝对值最大的那一行 t = i;
if (fabs(a[t][c]) < eps) continue;
for (int i = c; i < n + 1; i ++ ) swap(a[t][i], a[r][i]);//把绝对值最大的一行换到最上面 for (int i = n; i >= c; i -- ) a[r][i] /= a[r][c];//把该行第一个数变成1->倒着更新,不然第一个数先被更新成1除后面的数就无效了
for (int i = r + 1; i < n; i ++ ) if (fabs(a[i][c]) > eps) for (int j = n; j >= c; j -- ) a[i][j] -= a[r][j] * a[i][c];//把下边所有行的第c列消成0
r ++ ; }
for (int i = n - 1; i >= 0; i -- ) for (int j = i + 1; j < n; j ++ )//倒着把其他系数全部消成0(解所在位置的系数是1) a[i][n] -= a[j][n] * a[i][j];
#include<bits/stdc++.h> //#pragma GCC optimize(2) //#pragma GCC optimize("inline") //#pragma GCC optimize("-fgcse") //#pragma GCC target("avx","sse2") //#pragma GCC optimize("-fgcse-lm") //#pragma GCC optimize("-fipa-sra") //#pragma GCC optimize("-ftree-pre") //#pragma GCC optimize("-ftree-vrp") //#pragma GCC optimize("-fpeephole2") //#pragma GCC optimize("-ffast-math") //#pragma GCC optimize("-fsched-spec") //#pragma GCC optimize("unroll-loops") usingnamespace std; #define ll long long #define PII pair<int,int> #define PLL pair<ll,ll> #define fi first #define se second #define pb push_back #define debug(a) cout << #a << " " << a << '\n'; constint INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; constint N = 105; const ll mod = 1e9 + 7;
inline ll read();
ll A[N][N];
ll quick_pow(ll a, ll n){ ll ans = 1; while (n) { if (n & 1) ans = ans * a % mod; a = a * a % mod; n >>= 1; } return ans; }
ll inv(ll a){ returnquick_pow(a, mod - 2); }
ll gauss(int n){ ll ans = 1; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { if (A[j][i]) { for (int k = i; k <= n; k++) swap(A[i][k], A[j][k]); if (i != j) ans = -ans; break; } } if (!A[i][i]) return0; for (ll j = i + 1, iv = inv(A[i][i]); j <= n; j++) { ll t = A[j][i] * iv % mod; for (int k = i; k <= n; k++) A[j][k] = (A[j][k] - t * A[i][k] % mod + mod) % mod; } ans = (ans * A[i][i] % mod + mod) % mod; } return ans; }
int n; string s; voidsolve(){ cin >> n; cin >> s; int len = s.length(); ll ans = s[0] - '0'; for (int i = 1; i <len; i++) { ans = ((ans * 10)%mod + (s[i] - '0')) % mod; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> A[i][j]; } } ll res = gauss(n); // debug(res) // debug(ans) if (res == ans)cout << '+' << '\n'; else { cout << '-' << '\n'; }
}
intmain(){ ios::sync_with_stdio(false); #ifdef LOCAL int begin_time = clock(); freopen("../input.txt", "r", stdin); // freopen("../output.txt", "w", stdout); #endif int T = 1; cin >> T; for (int cas = 1; cas <= T; cas++) { #ifdef LOCAL printf("Case #%d: ", cas); #endif solve(); }
#ifdef LOCAL int end_time = clock(); printf("\nRun time: %.2lf ms", (double) (end_time - begin_time) / CLOCKS_PER_SEC * 1000); #endif
return0; }
inline ll read(){ char ch = getchar(); ll p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-')p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }
#include<bits/stdc++.h> //#pragma GCC optimize(2) //#pragma GCC optimize("inline") //#pragma GCC optimize("-fgcse") //#pragma GCC target("avx","sse2") //#pragma GCC optimize("-fgcse-lm") //#pragma GCC optimize("-fipa-sra") //#pragma GCC optimize("-ftree-pre") //#pragma GCC optimize("-ftree-vrp") //#pragma GCC optimize("-fpeephole2") //#pragma GCC optimize("-ffast-math") //#pragma GCC optimize("-fsched-spec") //#pragma GCC optimize("unroll-loops") usingnamespace std; #define ll long long #define PII pair<int,int> #define PLL pair<ll,ll> #define fi first #define se second #define pb push_back #define debug(a) cout << #a << " " << a << '\n'; constint INF = 0x3f3f3f3f; const ll LLINF =0x3f3f3f3f3f3f3f3f; const ll mod = 1e9 + 7; constdouble eps=1e-9; inline ll read(); int n; double a[10005],b[10005],c[10005]; doublef(double x){ double ans=0; for(int i =0;i<n;i++){ ans=max(ans,a[i]*x*x+b[i]*x+c[i]); } return ans; } voidsolve(){ n=read(); for(int i=0;i<n;i++) { a[i]=read(); b[i]=read(); c[i]=read(); } double l = 0,r = 1000; while(r-l>=eps){//精度问题 double m1 = l + (r-l)/3.0,m2 = r - (r-l)/3.0; if(f(m1)<f(m2)) r = m2; else l = m1; } printf("%.4lf\n",f(l));
} intmain(){ //ios::sync_with_stdio(false); #ifdef LOCAL int begin_time = clock(); freopen("../input.txt", "r", stdin); // freopen("../output.txt", "w", stdout); #endif int T = 1; T = read(); for (int cas = 1; cas <= T; cas++) { #ifdef LOCAL printf("Case #%d: ", cas); #endif solve(); }
#ifdef LOCAL int end_time = clock(); printf("\nRun time: %.2lf ms", (double) (end_time - begin_time) / CLOCKS_PER_SEC * 1000); #endif
return0; }
inline ll read(){ char ch = getchar(); ll p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-')p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }
boolcheck(int x){ ll cha = 0, cnt = x; int flag = 0; for (int i = 1; i <= n; i++) { if (w[i] > x * a) { cha = w[i] - x * a; cnt -= (cha + b - 1) / b;
} } if (cnt>=0)returntrue; else { returnfalse; } }
voidsolve(){ cin >> n >> a >> b; int ma = -1; for (int i = 1; i <= n; i++) { cin >> w[i]; ma = max(ma, w[i]); } ma = (ma + a - 1) / a; int l = 1, r = ma, mid; while (l < r) { mid = l + r >> 1; if (check(mid))r = mid; else l = mid + 1; } cout << l;
}
intmain(){ //ios::sync_with_stdio(false); #ifdef LOCAL int begin_time = clock(); freopen("../input.txt", "r", stdin); // freopen("../output.txt", "w", stdout); #endif int T = 1; // T = read(); for (int cas = 1; cas <= T; cas++) { #ifdef LOCAL printf("Case #%d: ", cas); #endif solve(); }
#ifdef LOCAL int end_time = clock(); printf("\nRun time: %.2lf ms", (double) (end_time - begin_time) / CLOCKS_PER_SEC * 1000); #endif
return0; }
inline ll read(){ char ch = getchar(); ll p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-')p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }
#define ll long long usingnamespace std; const ll maxn = 1e6 + 7; const ll N = 5e6 + 7; int n; vector<int> a; char s[maxn];
boolcheck(int x){ int now = 0; int sz = a.size(); for (int i = 0; i < sz - 1; i++) { if (now + x + 1 < a[i])returnfalse; if (now + x >= a[i]) { now = a[i] + x; } else { now = max(a[i], a[i] + x - 1);//取max是防止当x为0时,now反而还减小的情况 } if (now >= a[i + 1]) {//如果能扩的比右边那个1的位置还大,那就把now置为正好把a[i]~a[i+1]整段扩完 now = a[i + 1] - 1; } } if (now == n) { returntrue; } returnfalse; }
voidsolve(){ scanf("%d", &n); a.clear(); for (int i = 0; i < n; i++) { cin >> s[i]; if (s[i] == '1') { a.push_back(i + 1); } } a.push_back(n + 1); int l = 0, r = n - 1; while (l < r) { int mid = (l + r) >> 1; if (check(mid)) { r = mid; } else { l = mid + 1; } } printf("%d\n", l); }
intmain(){ int t = 1; scanf("%d", &t); while (t--) { solve(); } return0; }
boolcheck(ll x){ ll res = 0; for (ll i = 1; i <= n; i++) { ll a = x / i; res += min(a,m); } //debug(res); if (res >= k)returntrue; else { returnfalse; } }
voidsolve(){
cin >> n >> m >> k; ll l = 1, r = n * m, mid; while (l < r) { mid = l + r >> 1; //(mid); if (check(mid))r= mid; else l=mid+1; } cout <<l; }
intmain(){ //ios::sync_with_stdio(false); #ifdef LOCAL int begin_time = clock(); freopen("../input.txt", "r", stdin); // freopen("../output.txt", "w", stdout); #endif int T = 1; // T = read(); for (int cas = 1; cas <= T; cas++) { #ifdef LOCAL //printf("Case #%d: ", cas); #endif solve(); }
#ifdef LOCAL int end_time = clock(); printf("\nRun time: %.2lf ms", (double) (end_time - begin_time) / CLOCKS_PER_SEC * 1000); #endif
return0; }
inline ll read(){ char ch = getchar(); ll p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-')p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }
usingnamespace std; #define ll long long #define PII pair<int,int> #define PLL pair<ll,ll> #define fi first #define se second #define pb push_back #define debug(a) cout << #a << " " << a << '\n'; constint N = 1e5 + 5; constint M = 1e5 + 5; constint INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; const ll mod = 1e9 + 7;
inline ll read();
int n, m; ll a[N];
inlineboolcheck(ll x){ ll sum = 0, cnt = 1; for (int i = 0; i < n; i++) { if (sum + a[i] <= x)sum += a[i];//划分的每一段不能超过x else sum = a[i], cnt++; } return cnt <= m;//如果划分的每一段都不超过mid并且分的段数还小于m说明可以继续分 }
intmain(){ //ios::sync_with_stdio(false); ll l = -1, r = 0; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; l = max(l, a[i]);//注意二分的范围 r += a[i]; } //cout<<l<<' '<<r<<'\n'; while (l < r) { ll mid = l + r >> 1; //cout << mid << '\n'; if (check(mid))r = mid;//要继续分所以减小mid
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map> #include<unordered_map> #include<set> #pragma GCC optimize(2) usingnamespace std; #define ll long long #define PII pair<int,int> #define PLL pair<ll,ll> #define PLLL pair<ull,PLL> #define fi first #define se second #define pb push_back #define debug(a) cout << #a << " " << a << '\n'; constint N = 2e5 + 5; constint M = 1e5 + 5; constint INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; const ll mod = 1e9 + 7; #define ull unsigned long long
inline ll read();
ll n, m, k; PLL a[N];//a[i].first存值, a[i].second存从大到小排序后当前数和后一个数的差值
intmain(){ //ios::sync._with_stdio(false); n = read(), k = read(); ll ans = 0; ll sum = 0; for (int i = 0; i < n; i++) { a[i].fi = read(); ans += a[i].fi; } if (k >= ans) {//如果k比所有数字加起来都大,那直接计算所有数字1~a[i]的和 for (int i = 0; i < n; i++) { sum += (a[i].fi + 1) * a[i].fi / 2;//等差数列求和公式 } } else { sort(a, a + n); reverse(a, a + n); for (int i = 0; i < n - 1; i++) { a[i].se = (a[i].fi - a[i + 1].fi); } a[n - 1].se = a[n - 1].fi;
for (int i = 0; i < n; i++) { if (a[i].se * (i + 1) < k) {//第i大的数有i个 k -= a[i].se * (i + 1);//全部用完 sum += (a[i].se * a[i].fi - (a[i].se * (a[i].se - 1)) / 2) * (i + 1); //cout << sum << '\n'; } else { ll t = k / (i + 1); ll left = k % (i + 1);//这i个数同时等差数列求和到最后不一定都能用完 // cout<<a[i].fi<<'\n'; sum += (a[i].fi * t - t * (t - 1) / 2) * (i + 1); sum += left * (a[i].fi - t); k = 0; }
if (k == 0)break; } } cout << sum; }
inline ll read(){ char ch = getchar(); ll p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-')p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }
usingnamespace std; #define ll long long #define PII pair<int,int> #define PLL pair<ll,ll> #define fi first #define se second #define pb push_back #define debug(a) cout << #a << " " << a << '\n'; constint N = 1e5 + 5; constint M = 1e5 + 5; constint INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; const ll mod = 1e9 + 7;
inline ll read();
ll n, m; string s;
boolcheck(__int128 mid){ __int128 ans = 0; for (__int128 i = s.length() - 1, j = 1; i >= 0; i--, j *= mid) { if (j > m)returnfalse; ans += (s[i] - '0') * j; if (ans > m)returnfalse; } if (ans <= m)returntrue; else returnfalse; }
intmain(){ //ios::sync_with_stdio(false);
cin >> s >> m; int d = -1; for (auto i: s) { d = max(d, (i - '0')); } if (s.length() == 1) { if (s[0] - '0' <= m) { cout << 1; } else { cout << 0; } } else { ll l = d + 1, r = m; while (l < r) { //cout << l << ' ' << r << '\n'; ll mid = l + r + 1 >> 1; if (check(mid)) { l = mid; } else { r = mid - 1; }
} //cout << l << ' ' << r << '\n'; if (check(l))cout << l - d; else { cout << 0; }
} return0; }
inline ll read(){ char ch = getchar(); ll p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-')p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }
typedeflonglong LL; constint N = 2000010; int tot = 1, last = 1; structNode { int len, fa; int ch[26]; } node[N]; char str[N]; LL f[N]; int h[N], e[N], ne[N], idx; int pos[N];//记录后缀自动机中的节点对应原串中节点的位置 int vis[N]; int ans[N];
//对于节点u的子串集合 最长的是node[u].len ,最短的是node[node[u].fa].len+1 voidextend(int c, int id){ //fa:fail数组 ch:trie树 len:这个节点集合中字符串最长长度 int p = last, np = last = ++tot;//np:初步建立的后缀自动机 pos[np] = id; f[tot] = 1;//记录每个子串出现的次数 node[np].len = node[p].len + 1; for (; p && !node[p].ch[c]; p = node[p].fa) node[p].ch[c] = np; if (!p) node[np].fa = 1; else { int q = node[p].ch[c]; if (node[q].len == node[p].len + 1) node[np].fa = q; else { int nq = ++tot;//nq:当某节点没有合适的点做Fail的父亲时,需要的添加公共节点 pos[nq] = pos[q];//!!!!注意这里也要标记 node[nq] = node[q], node[nq].len = node[p].len + 1; node[q].fa = node[np].fa = nq; for (; p && node[p].ch[c] == q; p = node[p].fa) node[p].ch[c] = nq; } } }
voidadd(int a, int b){ e[idx] = b, ne[idx] = h[a], h[a] = idx++; }
voiddfs(int u, int dept){ vis[u] = 1; for (int i = 25; i >= 0; i--) {//直接硬搜 if (node[u].ch[i] && !vis[node[u].ch[i]]) { dfs(node[u].ch[i], dept + 1); } } if (!ans[pos[u]])ans[pos[u]] = pos[u] - dept + 1;//搜到底了,因为是从'z'到'a'搜,第一次搜到的一定是字典序最大的。 }
intmain(){ scanf("%s", str); for (int i = 0; str[i]; i++) extend(str[i] - 'a', i+1); memset(h, -1, sizeof h); dfs(1, 0); for (int i = 0; str[i]; i++)printf("%d %d\n", ans[i+1], i + 1);
constint N = 1000010; int n, m; char s[N]; int sa[N], x[N], y[N], c[N], rk[N], height[N]; int ans[N];
voidget_sa(){ for (int i = 1; i <= n; i++) c[x[i] = s[i]]++; for (int i = 2; i <= m; i++) c[i] += c[i - 1]; for (int i = n; i; i--) sa[c[x[i]]--] = i; for (int k = 1; k <= n; k <<= 1) { int num = 0; for (int i = n - k + 1; i <= n; i++) y[++num] = i; for (int i = 1; i <= n; i++) if (sa[i] > k) y[++num] = sa[i] - k; for (int i = 1; i <= m; i++) c[i] = 0; for (int i = 1; i <= n; i++) c[x[i]]++; for (int i = 2; i <= m; i++) c[i] += c[i - 1]; for (int i = n; i; i--) sa[c[x[y[i]]]--] = y[i], y[i] = 0; swap(x, y); x[sa[1]] = 1, num = 1; for (int i = 2; i <= n; i++) x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++num; if (num == n) break; m = num; } }
voidget_height(){ for (int i = 1; i <= n; i++) rk[sa[i]] = i; for (int i = 1, k = 0; i <= n; i++) { if (rk[i] == 1) continue; if (k) k--; int j = sa[rk[i] - 1]; while (i + k <= n && j + k <= n && s[i + k] == s[j + k]) k++; height[rk[i]] = k; } }
voidsolve(){ scanf("%s", s + 1); n = strlen(s + 1), m = 'z'; get_sa(); get_height(); int r=n; for(int i=n;i>=1;i--) { int st=sa[i]; int pos=i-1; while(sa[pos]>=sa[i])height[i]=min(height[i],height[pos]),pos--; if(st+height[i]>r)continue; for(int j=st+height[i];j<=r;j++)ans[j]=sa[i]; r=st+height[i]-1; i=pos+1; } for(int i=1;i<=n;i++){ cout<<ans[i]<<' '<<i<<'\n'; } }
intmain(){ //ios::sync_with_stdio(false); #ifdef LOCAL int begin_time = clock(); freopen("../input.txt", "r", stdin); // freopen("../output.txt", "w", stdout); #endif int T = 1; // T = read(); for (int cas = 1; cas <= T; cas++) { #ifdef LOCAL printf("Case #%d: ", cas); #endif solve(); }
#ifdef LOCAL int end_time = clock(); printf("\nRun time: %.2lf ms", (double) (end_time - begin_time) / CLOCKS_PER_SEC * 1000); #endif
return0; }
inline ll read(){ char ch = getchar(); ll p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-')p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }
voidcha(int l, int r, int v){ mp[l] += v; mp[r + 1] -= v; }
voidsolve(){ int n; cin >> n; int last = 0; //0代表(0,1)区间,1代表(1,2)区间 for (int i = 1; i <= n; i++) { int x; char direction; cin >> x >> direction;
if (direction == 'R') { cha(last, last + x - 1, 1); //cout<<last<<' '<<last+x<<'\n'; last = last + x; } else { cha(last - x, last - 1, 1); //cout<<last-x+1<<' '<<last<<'\n'; last = last - x; } //cout<<x<<' '<<direction<<'\n';
} int cnt = 0; int flag = 0; for (auto i: mp) { if (flag == 0) { flag = 1; sum[i.fi] += mp[i.fi]; continue; } sum[i.fi] = sum[i.fi - 1] + mp[i.fi]; }
int ans = 0; int l = mp.begin()->fi, r = mp.begin()->fi; for (auto i: mp) {
if (ans <= 1 && ans + i.se > 1) { l = i.fi;//找到满足条件的区间的左端点 } elseif (ans > 1 && ans + i.se <= 1) { r = i.fi;//找到满足条件的区间的右端点 cnt += r - l;//值 } ans += i.se; } cout << cnt; }
intmain(){ //ios::sync_with_stdio(false); #ifdef LOCAL int begin_time = clock(); freopen("../input.txt", "r", stdin); // freopen("../output.txt", "w", stdout); #endif int T = 1; // T = read(); for (int cas = 1; cas <= T; cas++) { #ifdef LOCAL printf("Case #%d: ", cas); #endif solve(); }
#ifdef LOCAL int end_time = clock(); printf("\nRun time: %.2lf ms", (double) (end_time - begin_time) / CLOCKS_PER_SEC * 1000); #endif
return0; }
inline ll read(){ char ch = getchar(); ll p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-')p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }