版本比较
CF1716
正文
增加 123 行删除 123 行
4 行未改动5678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713829 行未改动1681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252263 行未改动2302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802 行未改动4 行未改动观察样例不难发现只有全 $0$ 序列的答案才是 `NO`。```cpp#include <bits/stdc++.h>using namespace std;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";}int main(void) {ios::sync_with_stdio(0);int T; cin >> T;while (T--) solve();return 0;}```### B. Rebellion双指针前后扫,贪心填即可。```cpp#include <bits/stdc++.h>using namespace std;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;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;}cout << ans << "\n";}int main(void) {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;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);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);}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;}```### D. Paths on the Tree考虑做的暴力一点,设 `dfs(x, k)`代表 $x$ 的子树中填 $k$ 条路径。由于 $c$ 的限制,不难发现这是一个容斥原理的填写过程。因此状态数只有 $2n$ 个,直接暴力转移即可。```cpp#include <bits/stdc++.h>using namespace std;typedef long long i64;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 (k % c == 0) {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;}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";}int main(void) {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;int n;int f[105][105], x[105][105], y[105][105];int main(void) {ios::sync_with_stdio(0);memset(f, 0x3f, sizeof(f));for (int s = 0; s <= N; ++s)for (int i = 0; i <= 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;}}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 (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);} 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 : T) S.emplace_back(i);for (int i : S) {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;int n, m, a[300005], b[300005], B[600005], tot;struct Query {int op, l, r, k, ans;} Q[300005];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];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];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;}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];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);}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 行未改动4 行未改动5678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713829 行未改动1681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252263 行未改动2302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802 行未改动4 行未改动观察样例不难发现只有全 $0$ 序列的答案才是 `NO`。```cpp#include <bits/stdc++.h>using namespace std;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";}int main(void) {ios::sync_with_stdio(0);int T; cin >> T;while (T--) solve();return 0;}```### B. Rebellion双指针前后扫,贪心填即可。```cpp#include <bits/stdc++.h>using namespace std;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;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;}cout << ans << "\n";}int main(void) {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;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);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);}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;}```### D. Paths on the Tree考虑做的暴力一点,设 `dfs(x, k)`代表 $x$ 的子树中填 $k$ 条路径。由于 $c$ 的限制,不难发现这是一个容斥原理的填写过程。因此状态数只有 $2n$ 个,直接暴力转移即可。```cpp#include <bits/stdc++.h>using namespace std;typedef long long i64;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 (k % c == 0) {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;}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";}int main(void) {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;int n;int f[105][105], x[105][105], y[105][105];int main(void) {ios::sync_with_stdio(0);memset(f, 0x3f, sizeof(f));for (int s = 0; s <= N; ++s)for (int i = 0; i <= 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;}}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 (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);} 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 : T) S.emplace_back(i);for (int i : S) {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;int n, m, a[300005], b[300005], B[600005], tot;struct Query {int op, l, r, k, ans;} Q[300005];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];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];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;}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];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);}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 行未改动