版本比较
CF1716
正文
增加 123 行删除 123 行
4 行未改动567899101112111213141516171815161718192021222324252223242526272829303132333435353637383738394041424341424344454647484945464748495051515253545556575855565758596061626364656667686869707170717273747576747576777879787980818281828384858687888986878889909192939495969798991009910010110210310410210310410510610710810911010710810911011111211311211311411511611711811911511611711811912012112212312412512612712312412512612712812913013113213313413113213313413513613713829 行未改动1681691701711721731721731741751751761771781791791801811811821831841841851861871881891901901911921931941951941951961971981992002012022032042052062071981992002012022032042052062072082092102112092102112122132142152132142152162172182182192202212222202212222232242252263 行未改动2302312322332342352362342352362372382382392402412402412422432432442452462462472482492502502512522532542552562572522532542552562572582592602612622632612622632642652662672682662672682692702712722732742752762702712722732742752762772782792802 行未改动4 行未改动观察样例不难发现只有全 $0$ 序列的答案才是 `NO`。
```cpp#include <bits/stdc++.h>using namespace std; using namespace std;
int n, k; int a[55]; int n, k;int a[55];
void solve(void) { cin >> n >> k; int s = 0; for (int i = 1; i <= n; ++i) cin >> a[i], s += a[i]; if (s) cout << "YES\n"; else cout << "NO\n"; cin >> n >> k; int s = 0; for (int i = 1; i <= n; ++i) cin >> a[i], s += a[i]; if (s) cout << "YES\n"; else cout << "NO\n";}
int main(void) { ios::sync_with_stdio(0); int T; cin >> T; while (T--) solve(); return 0; ios::sync_with_stdio(0); int T; cin >> T; while (T--) solve(); return 0;}```
### B. Rebellion
双指针前后扫,贪心填即可。
```cpp#include <bits/stdc++.h>using namespace std; using namespace std;
int n; int a[100005]; int n;int a[100005];
void solve(void) { cin >> n; a[n + 1] = 1; for (int i = 1; i <= n; ++i) cin >> a[i]; int p = 0, q = n + 1, ans = 0; cin >> n; a[n + 1] = 1; for (int i = 1; i <= n; ++i) cin >> a[i]; int p = 0, q = n + 1, ans = 0; while (p < q) { while (p < n && a[p] == 0) ++p; while (q > 1 && a[q] == 1) --q; if (p >= q) break; swap(a[p], a[q]); ++ans; ++p; --q; while (p < n && a[p] == 0) ++p; while (q > 1 && a[q] == 1) --q; if (p >= q) break; swap(a[p], a[q]); ++ans; ++p; --q; } cout << ans << "\n"; cout << ans << "\n";}
int main(void) { ios::sync_with_stdio(0); int T; cin >> T; while (T--) solve(); return 0; ios::sync_with_stdio(0); int T; cin >> T; while (T--) solve(); return 0;}```
### C. Permutation Operations
由于自身是一个排列,因此直接从前往后扫,每个 $a_i<a_{i-1}$ 就在可以用的数中贪心选最小的能合法的数即可。
```cpp#include <bits/stdc++.h>using namespace std; using namespace std;
int n; int a[100005], p[100005]; int n;int a[100005], p[100005];
void solve(void) { set<int> s; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i], s.emplace(i); set<int> s; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i], s.emplace(i); for (int i = 2; i <= n; ++i) if (a[i] < a[i - 1]) { auto it = s.lower_bound(a[i - 1] - a[i]); p[*it] = i; s.erase(it); auto it = s.lower_bound(a[i - 1] - a[i]); p[*it] = i; s.erase(it); } for (int x : s) p[x] = 1; for (int i = 1; i <= n; ++i) cout << p[i] << " \n"[i == n]; for (int x : s) p[x] = 1; for (int i = 1; i <= n; ++i) cout << p[i] << " \n"[i == n];}
int main(void) { ios::sync_with_stdio(0); int T; cin >> T; while (T--) solve(); return 0; ios::sync_with_stdio(0); int T; cin >> T; while (T--) solve(); return 0;}```
### D. Paths on the Tree
考虑做的暴力一点,设 `dfs(x, k)`代表 $x$ 的子树中填 $k$ 条路径。由于 $c$ 的限制,不难发现这是一个容斥原理的填写过程。因此状态数只有 $2n$ 个,直接暴力转移即可。
```cpp#include <bits/stdc++.h>using namespace std; typedef long long i64; using namespace std;typedef long long i64;
int n, k, s[200005]; vector<int> G[200005]; map<pair<int, int>, i64> ans; int n, k, s[200005];vector<int> G[200005];map<pair<int, int>, i64> ans;
i64 dfs(int x, int k) { if (ans.find({x, k}) != ans.end()) return ans[{x, k}]; i64 &Ans = ans[{x, k}]; Ans = 1ll * s[x] * k; int c = G[x].size(); if (!c) return Ans; if (ans.find({x, k}) != ans.end()) return ans[{x, k}]; i64 &Ans = ans[{x, k}]; Ans = 1ll * s[x] * k; int c = G[x].size(); if (!c) return Ans; if (k % c == 0) { for (int y : G[x]) Ans += dfs(y, k / c); return Ans; for (int y : G[x]) Ans += dfs(y, k / c); return Ans; } vector<i64> f1(c), f2(c), diff(c); for (int i = 0; i < c; ++i) f1[i] = dfs(G[x][i], k / c), f2[i] = dfs(G[x][i], k / c + 1), diff[i] = f2[i] - f1[i], Ans += f1[i]; sort(diff.begin(), diff.end(), greater<i64>()); for (int i = 0; i < k % c; ++i) Ans += diff[i]; return Ans; vector<i64> f1(c), f2(c), diff(c); for (int i = 0; i < c; ++i) f1[i] = dfs(G[x][i], k / c), f2[i] = dfs(G[x][i], k / c + 1), diff[i] = f2[i] - f1[i], Ans += f1[i]; sort(diff.begin(), diff.end(), greater<i64>()); for (int i = 0; i < k % c; ++i) Ans += diff[i]; return Ans;}
void solve(void) { cin >> n >> k; ans.clear(); for (int i = 1; i <= n; ++i) G[i].clear(); for (int i = 2, u; i <= n; ++i) cin >> u, G[u].emplace_back(i); for (int i = 1; i <= n; ++i) cin >> s[i]; cout << dfs(1, k) << "\n"; cin >> n >> k; ans.clear(); for (int i = 1; i <= n; ++i) G[i].clear(); for (int i = 2, u; i <= n; ++i) cin >> u, G[u].emplace_back(i); for (int i = 1; i <= n; ++i) cin >> s[i]; cout << dfs(1, k) << "\n";}
int main(void) { ios::sync_with_stdio(0); int T; cin >> T; while (T--) solve(); return 0; ios::sync_with_stdio(0); int T; cin >> T; while (T--) solve(); return 0;}```
### E. Joking29 行未改动
```cpp#include <bits/stdc++.h>using namespace std;typedef vector<int> VI; const int N = 100; typedef vector<int> VI;const int N = 100;
int n; int n;int f[105][105], x[105][105], y[105][105];
int main(void) { ios::sync_with_stdio(0); ios::sync_with_stdio(0);
memset(f, 0x3f, sizeof(f)); memset(f, 0x3f, sizeof(f)); for (int s = 0; s <= N; ++s) for (int i = 0; i <= s; ++i) { int j = s - i; int j = s - i; if (s <= 2) { f[i][j] = 0; continue; } for (int x = 0; x <= i; ++x) for (int y = 0; y <= j; ++y) { int v = max(f[x + y][i - x], f[s - x - y][x]) + 1; if (v < f[i][j]) f[i][j] = v, ::x[i][j] = x, ::y[i][j] = y; f[i][j] = v, ::x[i][j] = x, ::y[i][j] = y; } }
cin >> n; VI S, T; for (int i = 1; i <= n; ++i) S.push_back(i); cin >> n; VI S, T; for (int i = 1; i <= n; ++i) S.push_back(i); while (S.size() + T.size() > 2) { int x = S.size() / 2, y = T.size() / 2; if (S.size() + T.size() <= N) x = ::x[S.size()][T.size()], y = ::y[S.size()][T.size()]; VI S0(S.begin(), S.begin() + x); VI S1(S.begin() + x, S.end()); VI T0(T.begin(), T.begin() + y); VI T1(T.begin() + y, T.end()); cout << "? " << S0.size() + T0.size() << " "; for (int i : S0) cout << i << " "; for (int i : T0) cout << i << " "; cout << endl; string res; cin >> res; S.clear(), T.clear(); if (S.size() + T.size() <= N) x = ::x[S.size()][T.size()], y = ::y[S.size()][T.size()]; VI S0(S.begin(), S.begin() + x); VI S1(S.begin() + x, S.end()); VI T0(T.begin(), T.begin() + y); VI T1(T.begin() + y, T.end()); cout << "? " << S0.size() + T0.size() << " "; for (int i : S0) cout << i << " "; for (int i : T0) cout << i << " "; cout << endl; string res; cin >> res; S.clear(), T.clear(); if (res == "YES") { for (int i : S0) S.push_back(i); for (int i : T0) S.push_back(i); for (int i : S1) T.push_back(i); for (int i : S0) S.push_back(i); for (int i : T0) S.push_back(i); for (int i : S1) T.push_back(i); } else { for (int i : S1) S.push_back(i); for (int i : T1) S.push_back(i); for (int i : S0) T.push_back(i); for (int i : S1) S.push_back(i); for (int i : T1) S.push_back(i); for (int i : S0) T.push_back(i); } } for (int i : T) S.emplace_back(i); for (int i : T) S.emplace_back(i); for (int i : S) { cout << "! " << i << endl; string res; cin >> res; if (res == ":)") return 0; cout << "! " << i << endl; string res; cin >> res; if (res == ":)") return 0; } return 0;}```3 行未改动将每个数哈希成一个随机数,然后树状数组维护区间和,如果能被 $k$ 整除那就应该是对的。正确率是 $\frac 1 k$,跑个 $30$ 轮即可。
```cpp#include <bits/stdc++.h>using namespace std; typedef long long i64; const i64 N = 1e13; using namespace std;typedef long long i64;const i64 N = 1e13;
int n, m, a[300005], b[300005], B[600005], tot; int n, m, a[300005], b[300005], B[600005], tot;struct Query { int op, l, r, k, ans; } Q[300005]; int op, l, r, k, ans;} Q[300005];
mt19937_64 Rand(time(0)); mt19937_64 Rand(time(0));inline i64 rnd(i64 l = 1, i64 r = N) { return uniform_int_distribution<i64>(l, r)(Rand); }
i64 C[300005]; i64 C[300005];inline void add(int x, i64 k) { for (; x <= n; x += x & -x) C[x] += k; }inline i64 sum(int x) { i64 r = 0; for (; x; x -= x & -x) r += C[x]; return r; }
i64 mp[600005]; i64 mp[600005];void solve(void) { memset(C, 0, sizeof C); for (int i = 1; i <= tot; ++i) mp[i] = rnd(); for (int i = 1; i <= n; ++i) add(i, mp[b[i] = a[i]]); for (int i = 1; i <= m; ++i) if (Q[i].op == 1) add(Q[i].l, -mp[b[Q[i].l]]), add(Q[i].l, mp[b[Q[i].l] = Q[i].k]); else if (Q[i].ans && (sum(Q[i].r) - sum(Q[i].l - 1)) % Q[i].k != 0) Q[i].ans = 0; memset(C, 0, sizeof C); for (int i = 1; i <= tot; ++i) mp[i] = rnd(); for (int i = 1; i <= n; ++i) add(i, mp[b[i] = a[i]]); for (int i = 1; i <= m; ++i) if (Q[i].op == 1) add(Q[i].l, -mp[b[Q[i].l]]), add(Q[i].l, mp[b[Q[i].l] = Q[i].k]); else if (Q[i].ans && (sum(Q[i].r) - sum(Q[i].l - 1)) % Q[i].k != 0) Q[i].ans = 0;}
int main(void) { ios::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i], B[++tot] = a[i]; ios::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i], B[++tot] = a[i];
for (int i = 1; i <= m; ++i) { cin >> Q[i].op; if (Q[i].op == 1) cin >> Q[i].l >> Q[i].k, B[++tot] = Q[i].k; else cin >> Q[i].l >> Q[i].r >> Q[i].k, Q[i].ans = ((Q[i].r - Q[i].l + 1) % Q[i].k == 0); cin >> Q[i].op; if (Q[i].op == 1) cin >> Q[i].l >> Q[i].k, B[++tot] = Q[i].k; else cin >> Q[i].l >> Q[i].r >> Q[i].k, Q[i].ans = ((Q[i].r - Q[i].l + 1) % Q[i].k == 0); } sort(B + 1, B + tot + 1); tot = unique(B + 1, B + tot + 1) - (B + 1); for (int i = 1; i <= n; ++i) a[i] = lower_bound(B + 1, B + tot + 1, a[i]) - B; for (int i = 1; i <= m; ++i) if (Q[i].op == 1) Q[i].k = lower_bound(B + 1, B + tot + 1, Q[i].k) - B; for (int op = 1; op <= 30; ++op) solve(); for (int i = 1; i <= m; ++i) if (Q[i].op == 2) cout << (Q[i].ans ? "YES" : "NO") << "\n"; return 0; sort(B + 1, B + tot + 1); tot = unique(B + 1, B + tot + 1) - (B + 1); for (int i = 1; i <= n; ++i) a[i] = lower_bound(B + 1, B + tot + 1, a[i]) - B; for (int i = 1; i <= m; ++i) if (Q[i].op == 1) Q[i].k = lower_bound(B + 1, B + tot + 1, Q[i].k) - B; for (int op = 1; op <= 30; ++op) solve(); for (int i = 1; i <= m; ++i) if (Q[i].op == 2) cout << (Q[i].ans ? "YES" : "NO") << "\n"; return 0;}```
### G. Olympiad Training2 行未改动