inlinevoidwork(){ int a = rd(), b = rd(), c = rd(); int n = max(0, rd() - a), m = max(0, rd() - b); puts(n + m > c ? "NO" : "YES"); }
B. Make It Increasing
给一个数列,每次操作把一个位置整除 \(2\) ,最少操作多少次使得数列严格递增?
从后往前倒推,答案是固定的,一直做到符合要求即可。
1 2 3 4 5 6 7 8 9 10 11
int a[57]; inlinevoidwork(){ int n = rd(), ans = 0; for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = n - 1; i; --i) if (a[i] >= a[i + 1]) { if (a[i + 1] == 0) {puts("-1"); return;} while (a[i] >= a[i + 1]) {++ans; a[i] = a[i] / 2;} } printf("%d\n", ans); }
string s; inlinevoidwork(){ cin >> s; int n = s.length(); int l = 0, r = n - 1; for (int i = 0; i < n; ++i) if (s[i] == '1') l = i; for (int i = l; i < n; ++i) if (s[i] == '0') {r = i; break;} printf("%d\n", r - l + 1); }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 200007
bool vis[N];
int n, rt, f[N], cnt;
vector<int> res[N], son[N];
voiddfs(int u, int bel){ res[bel].push_back(u); if (son[u].empty()) {++cnt; return;} dfs(son[u][0], bel); for (int i = 1; i < son[u].size(); ++i) dfs(son[u][i], son[u][i]); }
inlinevoidwork(){ n = rd(); cnt = 0; for (int i = 1; i <= n; ++i) {res[i].clear(); son[i].clear();} for (int i = 1; i <= n; ++i) { f[i] = rd(); if (f[i] == i) rt = i; else son[f[i]].push_back(i); } dfs(rt, rt); printf("%d\n", cnt); for (int i = 1; i <= n; ++i) if (!res[i].empty()) { printf("%d\n", (int)res[i].size()); for (auto j : res[i]) printf("%d ", j); puts(""); } }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 200007
vector<int> e[N], seq;
bool vis[N];
int n, k, x, y, f[N], ans;
voiddfs(int u, int fa, int dep){ f[u] = fa; if (u == y) ans = -dep; for (auto v : e[u]) if (v != fa) dfs(v, u, dep + 1); }
inlinevoidwork(){ seq.clear(); n = rd(); k = rd(); x = rd(); y = rd(); for (int i = 1; i <= n; ++i) vis[i] = 0, e[i].clear(); for (int i = 1; i <= k; ++i) seq.push_back(rd()); seq.push_back(y); for (int i = 1; i < n; ++i) { int u = rd(), v = rd(); e[u].push_back(v); e[v].push_back(u); } dfs(x, x, 0); vis[x] = 1; for (auto i : seq) for (int u = i; !vis[u]; u = f[u]) vis[u] = 1, ans += 2; printf("%d\n", ans); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 257
int a[N], s[N], f[N][N];
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + (a[i] = rd()); memset(f, 0x3f, sizeof(f)); f[0][0] = 0; for (int k = m; ~k; --k) for (int i = 0; i < n; ++i) for (int j = 0; j <= m - k; ++j) f[i + 1][j + k] = min(f[i + 1][j + k], f[i][j] + abs(j + k - s[i + 1])); printf("%d\n", f[n][m]); return0; }