#include<iostream> #include<algorithm> #include<string.h> #include<stdio.h> usingnamespacestd; #define ll long long constint maxn = 100005; int n, k; int a[maxn]; ll res = 0; inlineboolok(int x){ ll sum = 0, mina = 0x7f7f7f; int m = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (sum >= x) { mina = min(sum, mina); sum = 0; m++; } } if (m >= k) res = max(res, mina); return m >= k; } intmain(int argc, charconst *argv[]) { ll sum = 0; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", a+i); sum += a[i]; } ll l = 1, r = sum, mid; while (l <= r) { mid = (l+r)>>1; if (ok(mid)) l = mid+1; else r = mid-1; } printf("%lld\n", res); return0; }
#include<iostream> #include<queue> #include<algorithm> #include<string.h> #include<stdio.h> usingnamespacestd; #define ll long long constint maxn = 1005; int e[maxn][maxn]; int n, m; int nxt[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; int vis[maxn][maxn]; structnode { int x, y, step; node(int _x, int _y, int _step) { x = _x; y = _y; step = _step; } }; intbfs(int x, int y){ memset(vis, 0, sizeof vis); queue<node> q; q.push(node(x, y, 0)); while (!q.empty()) { node top = q.front(); q.pop();
for (int i = 0; i < 4; i++) { int nx = top.x+nxt[i][0]; int ny = top.y+nxt[i][1]; if (nx < 0 || nx >= n || ny < 0 || ny >= m || vis[nx][ny]) continue; if (e[nx][ny] == 2) { return top.step+1; } vis[nx][ny] = 1; q.push(node(nx, ny, top.step+1)); } } return0; } intmain(int argc, charconst *argv[]) { char str[maxn]; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", str); for (int j = 0; j < m; j++) e[i][j] = str[j] - '0'; } int mina = 0x3f3f3f3f; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) if (e[i][j] == 1) { mina = min(mina, bfs(i, j)+1); } } printf("%d\n", mina);
#include<iostream> #include<vector> #include<algorithm> #include<string.h> #include<stdio.h> usingnamespacestd; #define ll long long constint maxn = 100005; int n; int su[maxn]; vector<int> g[maxn]; vector<int> res;
voiddfs(int u, int v){ su[u] = 1; int maxa = 0; for (int i = 0; i < (int)g[u].size(); i++) { int e = g[u][i]; if (e == v) continue; dfs(e, u); su[u] += su[e]; maxa = max(maxa, su[e]); } maxa = max(maxa, n-su[u]); if (maxa < n-maxa) res.push_back(u); }
intmain(int argc, charconst *argv[]) { int x; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &x); if (x) { g[x].push_back(i); g[i].push_back(x); } } dfs(1, 0); sort(res.begin(), res.end()); printf("%d\n", (int)res.size()); for (int i = 0; i < (int)res.size(); i++) printf("%d\n", res[i]); return0; }