版本比较
集训题
正文
增加 1671 行删除 0 行
408 行未改动全部展开409410411412413414415416417418419420165 行未改动全部展开586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682354 行未改动全部展开10371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619408 行未改动全部展开 return 0;}```
## I - Interesting Problem (Easy Version)
## J - Delete Range Mex
# Day 2
## A 书架平衡
165 行未改动全部展开```
## D 星图档案
what 为什么我的题解消失了
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;using poly = vector <int>;const int maxn = 4005;int T, n, k;void solve() { cin >> n >> k; vector <int> w(n + 1), siz(n + 1), ans(n + 1); vector <poly> f(n + 1), g(n + 1), G(n + 1); for (int i = 1; i <= n; i++) { cin >> w[i]; } for (int i = 1, u, v; i < n; i++) { cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } auto merge = [&](poly A, int La, int Ra, poly B, int Lb, int Rb, int Lc, int Rc) -> poly{ poly C(Rc + 1, 0); for (int c = Lc; c <= Rc; c++) { int l = max(Lb, c - Ra), r = min(Rb, c - La); for (int b = l, a = c - l; b <= r; b++, a--) { C[c] = max(C[c], A[a] + B[b]); } } return C; }; auto dfs1 = [&](auto &&self, int u, int fa) -> void{ siz[u] = 1, f[u] = {0}; for (auto v : G[u]) { if (v == fa) continue; self(self, v, u); int Ra = min(siz[u] - 1, k), Rc = min(k, siz[u] + siz[v] - 1); f[u] = merge(f[u], 0, Ra, f[v], 0, f[v].size() - 1, 0, Rc); siz[u] += siz[v]; } f[u].resize(min(siz[u], k) + 1, 0); for (int i = min(siz[u], k); i >= 1; i--) f[u][i] = max(f[u][i], f[u][i - 1] + i * w[u]); return; }; auto dfs2 = [&](auto &&self, int u, int fa) -> void{ vector <int> son; for (auto v : G[u]) if (v != fa) son.push_back(v); int m = son.size(); vector <int> h(m + 2, 0); for (int i = m; i >= 1; i--) h[i] = h[i + 1] + siz[son[i - 1]]; vector <poly> suf(m + 2); suf[m + 1] = {0}; for (int i = m; i >= 1; i--) suf[i] = merge(suf[i + 1], 0, suf[i + 1].size() - 1, f[son[i - 1]], 0, f[son[i - 1]].size() - 1, 0, min(k, h[i])); int preL = max(0ll, k - siz[u]), preR = min(k, n - siz[u]); poly pre(preR + 1, 0); int preSz = n - siz[u]; for (int i = preL; i <= preR; i++) pre[i] = g[u][i]; for (int i = 1; i <= m; i++) { int v = son[i - 1], Lg = max(0ll, k - siz[v]), Rg = max(k, n - siz[v]), Le = max(0ll, Lg - 1), Re = Rg; poly E = merge(pre, preL, preR, suf[i + 1], 0, suf[i + 1].size() - 1, Le, Re); g[v].assign(Rg + 1, 0); for (int j = Lg; j <= Rg; j++) { g[v][j] = E[j]; if (j > 0) g[v][j] = max(g[v][j], E[j - 1] + j * w[u]); } int L = max(0ll, k - h[i + 1] - 1), R = min(k, preSz + siz[v]); pre = merge(pre, preL, preR, f[v], 0, f[v].size() - 1, L, R); preL = L, preR = R, preSz += siz[v]; self(self, v, u); } ans[u] = pre[k - 1] + k * w[u]; return; }; dfs1(dfs1, 1, 0), g[1] = {0}, dfs2(dfs2, 1, 0); for (int i = 1; i <= n; i++) { cout << ans[i] << ' '; } cout << '\n'; return;}signed main() { freopen("tree.in", "r", stdin); freopen("tree.out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> T; while (T--) solve(); return 0;}```
# Day 3
## A - 「KWOI R2」染色
354 行未改动全部展开## G - 铁路环游
## H - 一直在你身旁
# Day 4
## A - 01 on Tree
给定 $n$ 个节点的树,每个节点权值为 $0$ 或 $1$ 求树上节点的拓扑序使得组成序列的逆序对数最小。
不考虑拓扑序的限制,则 ```00..0111..1``` 是最优的 ,于是想到,有 $v[i] = 0$ 则一旦父亲被选了则立即选它,我们可以将其与父亲节点合并。
```fa[x] A1 A2 A3 x```
其中 $A1$ $A2$ $A3$ 应是 $x$ 的兄弟子树,每个兄弟子树应该有一个权值,套路 Exchange Argument 考虑相邻两个块 $a, b$ ,逆序对数量 $cnt_a1 * cnt_b0 < cnt_b1 * cnt_a0$ 时,显然 $a$ 在 $b$ 前面,那么每个节点的权值为 $\frac{cnt1}{cnt0}$ 。
用 set 维护权值,每次取最小的一个与父亲节点合并,用并查集维护答案。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int maxn = 2e5 + 5;int n, fa[maxn], w[maxn], p[maxn], cnt[maxn][2], vis[maxn], ans;struct node { int u, cnt0, cnt1; node(int a = 0, int b = 0, int c = 0) { u = a, cnt0 = b, cnt1 = c; } friend bool operator < (node a, node b) { return a.cnt1 * b.cnt0 < b.cnt1 * a.cnt0; }};multiset <node> st;int get(int x) { if (p[x] == x) return x; return p[x] = get(p[x]);}void merge(int u, int v) { u = get(u), v = get(v); if (u == v) return; ans += cnt[u][0] * cnt[v][1]; cnt[v][0] += cnt[u][0]; cnt[v][1] += cnt[u][1]; p[u] = v; return;}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 2; i <= n; i++) { cin >> fa[i]; } for (int i = 1; i <= n; i++) { cin >> w[i]; p[i] = i, cnt[i][w[i]]++; st.insert(node(i, cnt[i][0], cnt[i][1])); } while (st.size()) { int u = st.begin()->u; st.erase(st.begin()); if (vis[u]) continue; vis[u] = true; if (!fa[u]) continue; int v = get(fa[u]); merge(u, v); st.insert(node(v, cnt[v][0], cnt[v][1])); } cout << ans << '\n'; return 0;}```
## B - 皇后游戏
每个大臣有两个数 $a_i$ 和 $b_i$ 求一种排序顺序使得 $c_i = max(c_{i-1}, \sum_{j\le i} a_i) + b_i$ 最大,使用 Exchange Argument 来寻找贪心规则,若相邻的 $i < j$ 是优的,设 $i$ 前的 $a$ 之和为 $x$ , $i$ 前的 $c$ 为 $y$ 则较大的 $c$ 值:
$$\max(\max(y, x + a_i) + b_i, x + a_i + a_j) + b_j$$
$$\max(y + b_i + b_j, x + a_i + b_i + b_j, x + a_i + a_j + b_j)$$交换 $i$ $j$ 后为:
$$\max(y + b_i + b_j, x + a_j + b_i + b_j, x + a_i + a_j + b_i) $$
按照题意:
$$\max(x + a_i + b_i + b_j, x + a_i + a_j + b_j) \le \max(x + a_j + b_i + b_j, x + a_i + a_j + b_i)$$
消去 x 得: $$a_i + b_j + \max(b_i, a_j) \le a_j + b_i + \max(b_j, a_i)$$$$\max(a_j, b_i) - a_j - b_i \le \max(a_i, b_j) - a_i - b_j$$
因为 $\max(a, b) - a - b = -\min(a, b)$ 则:
$$-\min(a_j, b_i) \le -\min(a_i, b_j)$$ $$\min(a_i, b_j) \le \min(a_j, b_i) $$
小于等于不满足传递性,于是继续优化:
$\min(a_i, b_j) \le \min(a_j, b_i)$ 有以下三种可能:1. $a_i < b_i$ 且 $a_j < b_j$ ,此时要按 $a$ 升序排序2. $a_i = b_i$ 且 $a_j = b_j$ ,此时无限制3. $a_i > b_i$ 且 $a_j > b_j$ ,此时要按 $b$ 降序排序这样就满足传递性了。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int maxn = 1e5 + 5;int T, n;struct node { int a, b, d; void D() { if (a < b) d = -1; if (a > b) d = 1; if (a == b) d = 0; } friend bool operator < (node x, node y) { if (x.d != y.d) return x.d < y.d; if (x.d <= 0) return x.a < y.a; return x.b > y.b; }} w[maxn];void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> w[i].a >> w[i].b; w[i].D(); } sort(w + 1, w + n + 1); int sum = 0, c = 0; for (int i = 1; i <= n; i++) { sum += w[i].a; c = max(c, sum) + w[i].b; } cout << c << '\n'; return;}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; while (T--) solve(); return 0;}```
## C - Coins
有 $x+y+z$ 个人,每个人有 $a_i$ $b_i$ $c_i$ 个三种不同的钱,这三种分别要 $x$ $y$ $z$ 个,求最后最大总钱数
先简化问题,三种钱太麻烦了,我们将三元组 $(a_i,b_i,c_i)$ 变为二元组 $(a_i-c_i,b_i-c_i)$ ,即默认每个人都贡献第三种钱,然后选 $x$ 个人贡献 $a_i-c_i$ 个第一种和 $y$ 个人贡献 $b_i-c_i$ 个第二种。
朴素地想,按照 $a_i-b_i$ 升序排序,这样正着选 $a$ 应该更有利,倒着选 $b$ 也更有利,但是证以下发现这不符合贪心策略,所以引入反悔操作。
变成反悔贪心了,扫一遍数组,先加入 $a_i$ 若总数大于 $x$ 则减去先前选了的最小值,这样在第 $i$ 时刻就是最大值,我们记 $l_i$ 为前 $i$ 个 $a$ 中最大的 $x$ 个,同理倒着做 $b$ 记 $r_i$ 为后 $i$ 个中 $b$ 的最大的 $y$ 个。
最后答案就是将序列分为两段,前一段取 $x$ 个 $a$ 最大,后一段取 $y$ 个 $b$ 最大,这样贪心地取是最优的,为什么取的不会混在一起?因为每一次取得都是最大的,易证若混在一起一定能找到不混在一起的更优解。
```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int maxn = 1e5 + 5; priority_queue<int, vector<int>, greater<int>> q;int x, y, z, a[maxn], b[maxn], pos[maxn], c, res, l[maxn], r[maxn];bool cmp(int i, int j) {return a[i] - b[i] > a[j] - b[j];}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> x >> y >> z; for (int i = 1; i <= x + y + z; i++) { cin >> a[i] >> b[i] >> c; a[i] -= c, b[i] -= c, res += c, pos[i] = i; } sort(pos + 1, pos + x + y + z + 1, cmp); for (int i = 1; i <= x + y + z; i++) { l[i] = l[i - 1] + a[pos[i]], q.push(a[pos[i]]); if ((int)q.size() > x) l[i] -= q.top(), q.pop(); } while (q.size()) q.pop(); for (int i = x + y + z; i >= 1; i--) { r[i] = r[i + 1] + b[pos[i]], q.push(b[pos[i]]); if ((int)q.size() > y) r[i] -= q.top(), q.pop(); } int ans = -1e18; for (int i = x; i <= x + z; i++) { ans = max(ans, l[i] + r[i + 1]); } cout << ans + res << '\n'; return 0;}```
## D - Adjacent Sums (hard)
## E - Email Scheduling Optimization
给定数组 $a$ , $b$ 。给第 $i$ 家公司写邮件要 $a_i$ 分钟,发出后 $b_i$ 分钟得到应答,一次查询会修改 $a$ 或 $b$ 中的值,要回答最早收到所有回复的时间。
安排一个顺序,则显然答案为 $\max(\sum_{j=1}^{i}a_j+b_i)$ ,依旧 Exchange Argument 导出 $b_i$ 从大到小做最优。
按照 $b$ 从大到小存入线段树,我们想一想区间答案:
$$\begin{aligned}f_{l,r} &= \max_{i=l}^{r}(\sum_{j=l}^{r}a_j+b_i)\\&= \max(max_{i=l}^{k}(\sum_{j=l}^{r}a_j+b_i), \max_{i=k+1}^{r}(\sum_{j=l}^{r}a_j+b_i))\\&= \max(\max_{i=l}^{k}(\sum_{j=l}^{r}a_j+b_i), \sum_{j=l}^{k}a_j+\max_{i=k+1}^{r}(\sum_{j=k + 1}^{r}a_j+b_i))\\&= \max(f_{l,k}, f_{k+1,r}+\sum_{j=l}^{k}a_j)\end{aligned}$$
可以区间合并,用线段树维护即可,注意 $b$ 是降序的所以线段树的合并要反过来,应该是:
$$ans_u=\max(ans_{ls}+sum_{rs},ans_{rs})$$
```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int maxn = 1e6 + 5;int n, q, tot, lsh[maxn << 1], a[maxn], b[maxn], op[maxn], id[maxn], x[maxn], sum[maxn << 2], ans[maxn << 2], cnt[maxn << 2];inline int find(int x) { return lower_bound(lsh + 1, lsh + tot + 1, x) - lsh;} #define ls (i << 1)#define rs (ls | 1)#define mid (l + r >> 1)void update(int l, int r, int i, int pos, int val, int c) { if (l == r) { cnt[i] += c; sum[i] += val; ans[i] = (cnt[i] > 0 ? sum[i] + lsh[l] : 0); } else { if (pos <= mid) update(l, mid, ls, pos, val, c); else update(mid + 1, r, rs, pos, val, c); sum[i] = sum[ls] + sum[rs]; ans[i] = max(ans[ls] + sum[rs], ans[rs]); } return;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; lsh[++tot] = b[i]; } for (int i = 1; i <= q; i++) { cin >> op[i] >> id[i] >> x[i]; lsh[++tot] = x[i]; } sort(lsh + 1, lsh + tot + 1); tot = unique(lsh + 1, lsh + tot + 1) - lsh - 1; for (int i = 1; i <= n; i++) { b[i] = find(b[i]); update(1, tot, 1, b[i], a[i], 1); } for (int i = 1; i <= q; i++) { int pos = id[i]; x[i] = find(x[i]); if (op[i] == 1) { update(1, tot, 1, b[pos], lsh[x[i]] - a[pos], 0); a[pos] = lsh[x[i]]; } else { update(1, tot, 1, b[pos], -a[pos], -1); b[pos] = x[i]; update(1, tot, 1, b[pos], a[pos], 1); } cout << ans[1] << '\n'; } return 0;}```
## F - Price Combo
## G - 京都观光 / Sightseeing in Kyoto
一个 $n\times m$ 的网格,从 $(1,1)$ 走到 $(n,m)$ ,当你位于 $(x,y)$ 时,可以花费 $a_x$ 走到 $(x,y+1)$ 或花费 $b_y$ 走到 $(x+1,y)$ ,求最小花费。
首先 $n,m\le10^5$ 说明不能使用 DP 了,我们先来一个局部极小值的情况,现在要从 $(x_1,y_1)$ 走到 $(x_2,y_2)$ 只拐一次弯,怎样最小,显然最小值是:
$$\min(a_{x_1}(y_2-y_1)+b_{y_2}(x_2-x_1),a_{x_2}(y_2-y_1)+b_{y_1}(x_2-x_1))$$
即走 $x$ 和 $y$ 方向的两种方案,其中假设第一项比第二项更优,即:
$$a_{x_1}(y_2-y_1)+b_{y_2}(x_2-x_1) \le a_{x_2}(y_2-y_1)+b_{y_1}(x_2-x_1)$$
$$(b_{y_2}-b_{y_1})(x_2-x_1) \le (a_{x_2}-a_{x_1})(y_2-y_1)$$
$$\frac{b_{y_1}-b_{y_2}}{y_1-y_2} \le \frac{a_{x_1}-a_{x_2}}{x_1-x_2}$$
变成了一个类似斜率的东西,若先走 $y$ 再走 $x$ 要求 $y$ 方向的斜率更小。
这启发我们在斜率更小的方向贪心地移动,只需维护两个指针然后在 $x$ 和 $y$ 方向走斜率更小的即可。
具体的,应当维护两个方向上的下凸包,对它使用单调栈吧!
```cpp#include<bits/stdc++.h>#define int long long#define X first#define Y secondusing namespace std;using pii = pair <int, int>;const int maxn = 1e5 + 5;int n, m, a[maxn], b[maxn], ans, x = 1, y = 1;inline double slope(pii p, pii q) { return double(p.Y - q.Y) / double(p.X - q.X);}struct Hull { int top; pii st[maxn]; void insert(pii x) { while (top > 1 && slope(st[top - 1], st[top]) > slope(st[top - 1], x)) top--; st[++top] = x; return; }} A, B;inline void moveX() { ans += B.st[y].Y * (A.st[x + 1].X - A.st[x].X); x++;}inline void moveY() { ans += A.st[x].Y * (B.st[y + 1].X - B.st[y].X); y++;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; A.insert({i, a[i]}); } for (int j = 1; j <= m; j++) { cin >> b[j]; B.insert({j, b[j]}); } while (x < A.top || y < B.top) { if (x == A.top) { moveY(); } else if (y == B.top) { moveX(); } else if (slope(A.st[x + 1], A.st[x]) < slope(B.st[y + 1], B.st[y])) { moveX(); } else { moveY(); } } cout << ans << '\n'; return 0;}```
## H - Cardboard Box
## I - Range Increment
## J - Mr. Kitayuta vs. Bamboos
有 $n$ 个竹子,第 $i$ 个每天会长高 $h_i$ 米,每天可以选最多 $k$ 个竹子使其下降 $p$ 米,求最后最小的最高竹子高度。
看到最大值最小想到二分,二分最高的竹子高度 $H$ ,于是变成判定问题,所有竹子经过 $m$ 天后是否可以达到 $H$ 米高。正难则反,从最后一天开始倒过来看,每一个竹子都是 $H$ 米,每天会下降 $a_i$ 米,可以拔高其中的 $k$ 个 $p$ 米,很好的模拟,到第一天前若每一颗竹子都不小于 $h_i$ 高,则经过平移 $H$ 是一个合法的答案。
问题在于模拟的过程,不能让竹子在 $m$ 天后低于 $h_i$ 米且过程中不能低于 $0$ 米,则每次贪心地将最快要降为 $0$ 的竹子拔高,用小根堆维护最后一天不能达到 $h_i$ 米的竹子将要将为 $0$ 的天数,按题意倒序模拟即可。
```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int maxn = 1e5 + 5;int n, m, k, p, h[maxn], a[maxn], cnt[maxn], l, r, H;struct node { int day, id; friend bool operator < (node a, node b) { return a.day > b.day; }};bool check(int x) { priority_queue <node> q; memset(cnt, 0, sizeof cnt); for (int i = 1; i <= n; i++) { if (x - a[i] * m < h[i]) { q.push({x / a[i], i}); } } for (int i = 1; i <= m && q.size(); i++) { for (int j = 1; j <= k && q.size(); j++) { node u = q.top(); q.pop(); if (u.day < i) return false; cnt[u.id]++; if (x + cnt[u.id] * p - a[u.id] * m < h[u.id]) { q.push({(x + cnt[u.id] * p) / a[u.id], u.id}); } } } return q.empty();}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m >> k >> p; for (int i = 1; i <= n; i++) { cin >> h[i] >> a[i]; r = max(r, h[i] + m * a[i]); } while (l <= r) { int mid = l + r >> 1; if (check(mid)) r = mid - 1, H = mid; else l = mid + 1; } cout << H << '\n'; return 0;}```
## K - BZOJ3252 攻略
给定一颗树,选 $k$ 个根到叶子的路径,求其并集的点权和的最大值。
显然的一个贪心,设根到节点 $u$ 的价值和为 $s_u$ ,先取 $s_u$ 最大的一条链,然后将链上的点权清零并更新 $s$ 重复 $k$ 次即可。怎么求呢,因为有叶子节点这个限制,很难更新,于是想到直接维护所有 $s$ 即可,用树剖 + 数据结构可以做到,但时间复杂度不优,因为总共只有 $n$ 个节点,于是清零时可以一个一个节点清,遇到已经清零过的节点说明其祖先已经被清零了,退出即可,最多清 $n$ 次,每次在数据结构的帮助下只有 $O(\log n)$ 的复杂度。
具体看线段树,要做到子树减去节点的贡献和求最大值的位置,事实上我们只用维护最大值即可,用线段树上二分求位置。
```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int maxn = 2e5 + 5;int n, k, w[maxn], u, v, ans, tot, root, vis[maxn], siz[maxn], son[maxn], fat[maxn], sum[maxn], arr[maxn], ide[maxn];vector <int> G[maxn];int t[maxn << 2], tag[maxn << 2];void dfs1(int u, int fa) { siz[u] = 1, fat[u] = fa; for (auto g : G[u]) { if (g == fa) continue; dfs1(g, u); siz[u] += siz[g]; if (siz[g] > siz[son[u]]) son[u] = g; }}void dfs2(int u, int S) { ide[u] = ++tot, arr[tot] = u, sum[u] = S + w[u]; if (son[u]) dfs2(son[u], S + w[u]); for (auto g : G[u]) { if (g == fat[u] || g == son[u]) continue; dfs2(g, S + w[u]); }}#define ls (i << 1)#define rs (ls | 1)#define mid (l + r >> 1) void build(int l, int r, int i) { if (l == r) { t[i] = sum[arr[l]]; } else { build(l, mid, ls); build(mid + 1, r, rs); t[i] = max(t[ls], t[rs]); }}void apply(int i, int w) {t[i] += w, tag[i] += w;}void pushdown(int i) {apply(ls, tag[i]), apply(rs, tag[i]), tag[i] = 0;}void update(int l, int r, int i, int a, int b, int x) { if (a <= l && r <= b) { apply(i, x); } else { pushdown(i); if (a <= mid) update(l, mid, ls, a, b, x); if (mid < b) update(mid + 1, r, rs, a, b, x); t[i] = max(t[ls], t[rs]); }}pair <int, int> query(int l, int r, int i) { if (l == r) { return {arr[l], t[i]}; } else { pushdown(i); if (t[ls] >= t[rs]) return query(l, mid, ls); else return query(mid + 1, r, rs); }}int faa[maxn];signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> w[i]; } for (int i = 1; i < n; i++) { cin >> u >> v; faa[v] = u; G[u].push_back(v); G[v].push_back(u); } for (int i = 1; i <= n; i++) { if (!faa[i]) root = i; } dfs1(root, 0), dfs2(root, 0), build(1, n, 1); while (k--) { pair <int, int> qu = query(1, n, 1); u = qu.first, ans += qu.second; while (!vis[u] && u) { update(1, n, 1, ide[u], ide[u] + siz[u] - 1, -w[u]); w[u] = 0, vis[u] = 1; u = fat[u]; } } cout << ans << '\n'; return 0;}```
## L - Gap Swap (easy)
## M - Gap Swap (hard)
## N - Difficult Mountain
有 $n$ 个人第 $i$ 个人有两个属性 $s_i$ 和 $a_i$ ,现在要攀登一座难度为 $d$ 的张雪峰,当 $d\le s$ 时可以成功攀登并使 $d=\max(d,a_i)$ ,问最优的顺序可以使多少人登山?
将人分为两个集合 $A={i|a_i\le s_i}$ 和 $B={i|s_i<a_i}$ 因为 $A$ 的人可以全部都选,但是 $B$ 中的人选了会导致一些人选不了,所以选 $A$ 更优,必须先选。
看剩下的人,按照 $\max(s_i,a_i)$ 排序,相等则按 $s_i$ 升序排序即可,瞪眼难,证明随便证就行。
```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int maxn = 5e5 + 5;int n, d, cnt;struct node { int s, a; friend bool operator < (node x, node y) { if (max(x.s, x.a) != max(y.s, y.a)) return max(x.s, x.a) < max(y.s, y.a); return x.s < y.s; }} p[maxn];signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> d; for (int i = 1; i <= n; i++) { cin >> p[i].s >> p[i].a; } sort(p + 1, p + n + 1); for (int i = 1; i <= n; i++) { if (p[i].s >= d) { d = max(d, p[i].a); cnt++; } } cout << cnt << '\n'; return 0;}```
## O - Modulo Pairing
将长为 $2n$ 的值域为 $[0,m)$ 的序列 $a$ 分为 $n$ 个二元组 $(x,y)$ 求 $(x+y)\mod m$ 的最小值。
首先数据范围太大不考虑 DP 那就是贪心。考虑没有 $m$ 的限制怎么做,显然是第 $i$ 小和第 $i$ 大的数配对,但是加了 $m$ 的限制会导致一些权值变小,但这依然启发我们先将 $a$ 排序。
依旧 Exchange Argument 解法,将 $a\le b\le c\le d$ 列出来发现 $(a,c)$ $(b,d)$ 的组合一定是不优的,所以数对之间的关系只能是并列或包含。
将数对又分为 $x+y<m$ 和 $x+y\ge m$ 两部分,同样进行分类讨论,一个减去 $m$ 的配对不优,大概可以知道最优配对的样式了:最优配对是分为两个部分,一个部分只有包含关系,即第 $i$ 小加上第 $i$ 大。
分界点越左越好,二分这个分界点即可。
```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int maxn = 2e5 + 5;int n, m, a[maxn];bool check(int pos) { for (int i = pos + 1, j = n; i <= j; i++, j--) { if (a[i] + a[j] < m) { return false; } } return true;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; n <<= 1; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); int l = 0, r = n >> 1, ans; while (l <= r) { int mid = l + r >> 1; if (check(mid << 1)) ans = mid << 1, r = mid - 1; else l = mid + 1; } int sum = 0; for (int i = 1, j = ans; i <= j; i++, j--) sum = max(sum, a[i] + a[j]); for (int i = ans + 1, j = n; i <= j; i++, j--) sum = max(sum, a[i] + a[j] - m); cout << sum << '\n'; return 0;}```
## P - Flip the Cards
# Day 5
## A - The table
将一个 $n \times m$ 的矩阵进行操作,一次操作可以将一行/列的所有数取反,求使所有数总和最大的方案,其中 $1\le n,m\le 100$ 且 $|a_{i,j}|\le100$ 。
首先发现行列的操作可以任意交换顺序,因此不用担心操作先后问题,贪心的想,只要做将会使答案增加的操作即可。
将 $n$ $m$ $\max(a_{i,j})$ 视为同阶,分析时间复杂度,一个优的操作最多使一个元素增加 $O(1)$ 而总共最多增加 $O(n^2)$ ,则最多操作 $O(n^2)$ 次,一次操作检查每个行/列一次,时间复杂度为 $O(n^4)$
```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int maxn = 105;int n, m, a[maxn][maxn], visx[maxn], visy[maxn]; bool check1() { int pos = 0; for (int i = 1; i <= n; i++) { int sum = 0; for (int j = 1; j <= m; j++) sum += a[i][j]; if (sum < 0) {pos = i; break;} } if (pos) {visy[pos] ^= 1; for (int j = 1; j <= m; j++) a[pos][j] *= -1;} return pos;}bool check2() { int pos = 0; for (int j = 1; j <= m; j++) { int sum = 0; for (int i = 1; i <= n; i++) sum += a[i][j]; if (sum < 0) {pos = j; break;} } if (pos) {visx[pos] ^= 1; for (int i = 1; i <= n; i++) a[i][pos] *= -1;} return pos;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; while (check1() || check2()) {} vector <int> X, Y; for (int i = 1; i <= n; i++) if (visy[i]) X.push_back(i); for (int j = 1; j <= m; j++) if (visx[j]) Y.push_back(j); cout << X.size() << ' '; for (auto ans : X) cout << ans << ' '; cout << '\n'; cout << Y.size() << ' '; for (auto ans : Y) cout << ans << ' '; cout << '\n'; return 0;}```
## B - Catshock
有 $n$ 个节点的树,猫在节点 $1$ 而人在节点 $n$ 。有两种操作,操作 $1$ 让猫随机到达相邻节点,操作 $2$ 删除一个节点和它连的边,寻找一个操作方案使得猫到达 $n$ ,不能连续两个操作 $2$ ,答案长度不超过 $3n$ 。
猫会乱走,应当限制。看到 $3n$ 启发我们让猫走两步在删除一个点,此时猫的限制是猫到 $1$ 的距离一定为偶数,所以只要删除到 $1$ 距离为奇数的叶子节点既可以限制猫,又不会删除猫或使猫与人不在一个连通块。那没有偶数距离的叶子节点了怎么办,这时让猫再走一步,同理删除奇数距离叶子即可。最后树变为一奇一偶,判断猫是否在 $n$ 即可。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int maxn = 2e5 + 10;int T, n, u, v, vis[maxn], dis[maxn], deg[maxn];vector <int> G[maxn], path;bool dfs(int u, int fa) { if (u == n) { path.push_back(u); vis[u] = true; } for (auto v : G[u]) { if (v == fa) continue; dis[v] = dis[u] ^ 1; if (dfs(v, u)) { path.push_back(u); vis[u] = true; } } return vis[u];}void solve() { cin >> n; path.clear(); for (int i = 1; i <= n; i++) { G[i].clear(); dis[i] = deg[i] = 0; vis[i] = false; } for (int i = 1; i < n; i++) { cin >> u >> v; deg[u]++, deg[v]++; G[u].push_back(v); G[v].push_back(u); } dfs(1, 0); reverse(path.begin(), path.end()); queue <int> que; for (int i = 1; i <= n; i++) if (deg[i] == 1 && !vis[i]) que.push(i), vis[i] = true; int step = 0; vector <pair <int, int>> ans; while (!que.empty()) { int u = que.front(); que.pop(); ans.push_back({1, 0}), step ^= 1; if (step == dis[u]) ans.push_back({1, 0}), step ^= 1; ans.push_back({2, u}); for (auto g : G[u]) { if (vis[g]) continue; deg[g]--; if (deg[g] == 1) vis[g] = true, que.push(g); } } for (int i = 0; i < path.size() - 1; i++) { ans.push_back({1, 0}); step ^= 1; if (dis[path[i]] == step) { ans.push_back({1, 0}); step ^= 1; } ans.push_back({2, path[i]}); } cout << ans.size() << '\n'; for (auto S : ans) { if (S.first == 1) cout << S.first << '\n'; else cout << S.first << " " << S.second << '\n'; } return;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> T; while (T--) solve(); return 0;}```
## C - Prime Problem
将 $n$ 个数分为尽量小的集合,使得每个集合元素之和为素数。
发现哥德巴赫猜想至少在 OI 的数据范围内一定成立,即一个大于 $2$ 的偶数一定可以拆分为两个素数之和,于是设 $S=\sum_{i=1}^ni$ 然后大力分类讨论。
$S$ 是素数时,整体为一个集合。
$S$ 是偶数时,将其拆分为两个素数,可以发现较小的 $p$ 不大于 $n$ ,此时将其分为 ${p}$ 和 ${p}$ 的补集。
$S$ 是奇数时,若可以拆分为 $2$ 和一个奇素数之和,则分为 ${2}$ 与其补集;否则拆分为 $3$ 和一个偶数之和,然后像情况二一样做即可。
```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int maxn = 6005;int n, S;bool check(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true;}int find(int x) { for (int i = 2; i <= n; i++) { if (check(i) && check(x - i)) { return i; } } return 0;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n; S = n * (n + 1) / 2; if (check(S)) { for (int i = 1; i <= n; i++) cout << 1 << ' '; } else if (S & 1) { if (check(S - 2)) { for (int i = 1; i <= n; i++) cout << (i == 2 ? 2 : 1) << ' '; return 0; } int x = find(S - 3); for (int i = 1; i <= n; i++) { if (i == 3) cout << 3 << ' '; else if (i == x) cout << 2 << ' '; else cout << 1 << ' '; } } else { int x = find(S); for (int i = 1; i <= n; i++) cout << (i == x ? 2 : 1) << ' '; } return 0;}```
## D - Prefix Product Sequence
给定 $n$ 求一个 $[1,n]$ 的排列 $a$ 使其模 $n$ 意义下的前缀乘积为一个 $[0,n)$ 的排列。
事实上在模 $n$ 意义下 $[1,n]$ 的排列等价于 $[0,n)$ 的排列。设前缀乘积为 $s$ 则不存在不同的整数 $i$ $j$ 使得 $s_i=s_j$ ,即 $a$ 不存在任意一个子段使得其乘积为 $1$ 或 $0$ ,那么显然 $1$ 要放在最开头, $n$ 放在结尾。
考虑序列中有一段是 $(n-1)! \mod n \neq 1$ ,这个条件当且仅当 $n$ 是大于 $4$ 的合数无解,即 $n$ 为 $1$ 或 $2$ 或 $3$ 或 $4$ 或更大的素数。考虑 $n$ 为素数怎么做,只要放入 $\frac{i1}{i-1}$ 就能做到没有重复。
```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int maxn = 1e5 + 5;int n;bool check(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true;}int ksm(int a, int b) { int res = 1; while (b) { if (b & 1) res = res * a % n; a = a * a % n, b >>= 1; } return res;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n; if (n == 1) { cout << "YES\n1\n"; } else if (n == 2) { cout << "YES\n1\n2\n"; } else if (n == 3) { cout << "YES\n1\n2\n3\n"; } else if (n == 4) { cout << "YES\n1\n3\n2\n4\n"; } else { if (check(n)) { cout << "YES\n"; for (int i = 1; i <= n; i++) { if (i == 1 || i == n) { cout << i << '\n'; } else { cout <<i * ksm(i - 1, n - 2) % n << '\n'; } } } else { cout << "NO\n"; } } return 0;}```
## E - Double Knapsack
给定两个集合 $A$ $B$ 求出两个集合元素之和相等的子集(值域为 $[1,n]$ )。
很妙的做法,设两个子集之和的差值为 $x$ 若,哪边小了就随便加一个进去,若有解那么做 $2n$ 次一定可以得到,因为差值的值有 $2n$ 个而总共有 $2n+1$ 个差值,则一定存在至少两个重复的差值,那么做差就是答案。
```cpp#include <bits/stdc++.h>#define int long long using namespace std;const int maxn = 2e6 + 5;int n, a[maxn], b[maxn], x, p1, p2;unordered_map <int, pair <int, int>> mp;signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; int T = n << 1; mp[0] = {0, 0}; while (T--) { if (x < 0) x += a[++p1]; else x -= b[++p2]; if (mp.count(x)) { int s1 = mp[x].first, s2 = mp[x].second; cout << p1 - s1 << '\n'; for (int i = s1 + 1; i <= p1; i++) cout << i << ' '; cout << '\n'; cout << p2 - s2 << '\n'; for (int i = s2 + 1; i <= p2; i++) cout << i << ' '; cout << '\n'; return 0; } mp[x] = {p1, p2}; } cout << -1 << '\n'; return 0;}```
## F - Square Root of Permutation
有两个 $[1,n]$ 的排列 $q$ 和 $p$ 满足 $q_{q_i}=p_i$ 已知 $p$ 求 $q$ 。
将 $i$ 向 $p_i$ 连边,那么整个图应为若干个环,尝试将一个环做这样的操作,发现若是奇环,则环不变,否则会变成两个大小相等的环,其相邻元素不在同一个环。对于 $p$ 只要看其中的偶环按照大小两两配对是否可行,奇环
具体的,奇环变为 ```1 3 5 ... k 2 4 6 ... k-1``` 偶环变为 ```1 3 5 ... k-1``` 和 ```2 4 6 ... k``` 。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int maxn = 1e6 + 5; int n, tot, p[maxn], q[maxn], vis[maxn];vector <int> G[maxn], len[maxn];bitset <maxn> bit;void solve1(vector <int> vec) { int len = vec.size(); vector <int> tmp; tmp.push_back(vec[0]); for (int i = 1, j = ((len - 1) >> 1) + 1; j < len; i++, j++) tmp.push_back(vec[j]), tmp.push_back(vec[i]); q[tmp[len - 1]]=tmp[0]; for (int i = 1; i < len; i++) q[tmp[i - 1]] = tmp[i];}void solve2(vector <int> v1, vector <int> v2) { int len = v1.size(); vector <int> tmp; for (int i = 0; i < len; i++) tmp.push_back(v1[i]), tmp.push_back(v2[i]); q[tmp[(len << 1) - 1]] = tmp[0]; for (int i = 1; i < (len << 1); i++) q[tmp[i - 1]] = tmp[i];}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> p[i]; } for (int i = 1; i <= n; i++) { if (vis[i]) continue; int u = i; tot++; while (!vis[u]) { G[tot].push_back(u); vis[u] = 1, u = p[u]; } len[G[tot].size()].push_back(tot); } for (int i = 1; i <= n; i++) { if (!len[i].size()) continue; if (!(i & 1) && (len[i].size() & 1)) { cout << "-1\n"; return 0; } if (i & 1) { for (auto j : len[i]) { solve1(G[j]); } } else { for (int j = 0; j < len[i].size(); j += 2) { solve2(G[len[i][j]], G[len[i][j + 1]]); } } } for (int i = 1; i <= n; i++) { cout << q[i] << ' '; } return 0;}```
## G - Hack it!
定义 $f(x)$ 为十进制下 $x$ 个各数位之和,给定模数 $a$ 求满足 $\sum_{i=l}^{r}f(i)\equiv 0 \mod a$ 的 $[,l r]$ ,答案在 $10^{200}$ 内。
设 $p \equiv \sum_{i=0}^{10^{18}-1}f(i)$ ,设 $g(x) = \sum_{i=x}^{10^{18}+x-1}f(i)$ 易得 $g(x)\equiv p+x$ 则可以构造一组解为 $[a-p,a-p+10^{18}-1]$ 。
现在来求 $p$ 。每个数位上的数都平均出现,有 $18$ 个数位,数之和为 $45$ 排列组合后得 $p=18\times45\times10^{17}=81\times10^{18}\mod a$ 。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int a, p, inf = 1e18;int ksm(int x, int b) { int res = 0; while (b) { if (b & 1) (res += x) %= a; (x += x) %= a, b >>= 1; } return res;}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> a; p = ksm(inf, 81); cout << a - p << ' ' << a - p + inf - 1 << '\n'; return 0;}```
## H - Bitwise Reversion
给定三个数 $x$ $y$ $z$ 问是否有三个数 $a$ $b$ $c$ 使得 $a&b=x$ $b&c=y$ &a&c=z& 。
唐题,显然每个位的答案是没有关联的,一个一个位处理即可,现在假设这 $(x,y,z)$ 第 $i$ 位的集合为 $S_i$ , $(a,b,c)$ 的第 $i$ 位的集合为 $A_i$ 。
若 $S_i={0,0,0}$ 则 $A_i={0,0,0}$ 。若 $S_i={0,0,1}$ 则 $A_i={0,1,1}$ 。若 $S_i={0,1,1}$ 则 $A_i$ 无解。若 $S_i={1,1,1}$ 则 $A_i={1,1,1}$ 。
综上,一旦存在 $S_i={0,1,1}$ 则无解,否则有解。
```cpp#include <bits/stdc++.h>#define int long long using namespace std;int T, x, y, z;bool check(int x, int y, int z) { for (int i = 0; i < 31; i++) { int cnt = ((x >> i) & 1) + ((y >> i) & 1) + ((z >> i) & 1); if (cnt == 2) return false; } return true;}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; while (T--) { cin >> x >> y >> z; if (check(x, y, z)) cout << "Yes\n"; else cout << "No\n"; } return 0;}```
## I - Symmetrical Polygons
给定 $n$ 条边,选取其中的一些组成一个轴对称的非退化凸多边形,求其最大边长。
首先将相同长度的边两两一对贪心加入,剩下来是没有组对的边,可以选 $0$ 或 $1$ 或 $2$ 条,类比等腰三角形满足边小于两腰之和,要周长最大,只需去最大和满足的次大即可。
```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int maxn = 2e5 + 5;int T, n, a[maxn];map <int, int> mp;void solve() { mp.clear(); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; mp[a[i]]++; } queue <int> q1; priority_queue<int, vector<int>, greater <int>> q2; int sum = 0; for (int i = 1; i <= n; i++) { int k = mp[a[i]]; if (k > 1) { for (int j = 1; j <= (k >> 1); j++) { mp[a[i]] -= 2; q1.push(a[i]); sum += a[i]; } } if (mp[a[i]]) q2.push(a[i]); mp[a[i]] = 0; } int lst = 0, ans = 0; if (q1.size() != 1) ans = sum << 1; while (q2.size()) { int x = q2.top(); q2.pop(); if ((sum << 1) + lst > x) ans = max(ans, (sum << 1) + lst + x); lst = x; } cout << ans << '\n'; return;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> T; while (T--) solve(); return 0;}```
## J - Not Alone
给定长为 $n$ 的环形序列,一次操作可以使一个元素 $+1$ 或 $-1$ ,至少多少次操作可以使序列任意一项等于相邻一项。
首先想到破环为链,把相等的当作一个联通块,很容易想到极小连通块大小是 $2$ 个或 $3$ 个,先在这个基础上做 DP 然后修缮环处的答案。
分类讨论开头结尾的情况,不多,一种情况做一次 DP 最后取最小值。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int maxn = 2e5 + 5;int T, n, ans, a[maxn], f[maxn];void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; if (i <= 4) a[n + i] = a[i]; } ans = 1e18; for (int i = 0; i <= 3; i++) { for (int j = 0; j <= n + i; j++) f[j] = 1e18; f[i] = 0; for (int j = i + 1; j <= n + i; j++) { if (j >= i + 2) f[j] = min(f[j], f[j - 2] + abs(a[j] - a[j - 1])); if (j >= i + 3) f[j] = min(f[j], f[j - 3] + max({a[j - 2], a[j - 1], a[j]}) - min({a[j - 2], a[j - 1], a[j]})); } ans = min(ans, f[n + i]); } cout << ans << '\n';}signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; while (T--) solve(); return 0;}```
# Day 6
## A 爆破
## B 分组
## C 双子之争
## D 并查集
# Day 8
## A - 蚯蚓排队
## B - 最长同类子串
给定两个字符串 $s$ 和 $t$ ,求最大长度 $k$ 使得两个字符串中存在长为 $k$ 的子串 $s'$ 和 $t'$ 使其本质相同,即若 $s'_i=s'_j$ 则 $t'_i=t'_j$ 。
先想到二分 $k$ 然后将问题转化为判定问题,设计一种多项式哈希,第 $i$ 个数的权值为其到前一个相同元素的距离,若没有则为 $0$ ,这不能使用前缀和求子串哈希,只能用滑动窗口动态维护。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;using ull = unsigned long long;const int maxn = 1e5 + 5, base = 998244353;int n, m, preS[maxn], preT[maxn], nxtS[maxn], nxtT[maxn];ull pw[maxn] = {1};char s[maxn], t[maxn];void build() { for (int i = 1; i < maxn; i++) pw[i] = pw[i - 1] * base; auto make1 = [&](char *str, int len, int *pre) -> void{ vector <int> lst(26, 0); for (int i = 1; i <= len; i++) { pre[i] = lst[str[i] - 'a']; lst[str[i] - 'a'] = i; } return; }; auto make2 = [&](char *str, int len, int *pre) -> void{ vector <int> lst(26, len + 1); for (int i = len; i >= 1; i--) { pre[i] = lst[str[i] - 'a']; lst[str[i] - 'a'] = i; } return; }; make1(s, n, preS), make1(t, m, preT); make2(s, n, nxtS), make2(t, m, nxtT); return;}bool check(int k) { auto calc = [&](char *str, int len, int *pre, int *nxt) -> vector <ull>{ vector <ull> ret, w(len + 1); ull now = 0; for (int i = 1; i <= k; i++) { w[i] = (pre[i] <= i - k || !pre[i] ? 0 : i - pre[i]); (now *= base) += w[i]; } ret.push_back(now); for (int i = k + 1; i <= len; i++) { w[i] = (pre[i] <= i - k || !pre[i] ? 0 : i - pre[i]); now = now * base - w[i - k] * pw[k]; if (nxt[i - k] <= i) { now -= w[nxt[i - k]] * pw[i - nxt[i - k]]; w[nxt[i - k]] = 0; } now += w[i]; ret.push_back(now); } return ret; }; vector <ull> hshS = calc(s, n, preS, nxtS), hshT = calc(t, m, preT, nxtT); unordered_map <ull, bool> mp; for (auto tmp : hshS) mp[tmp] = true; for (auto tmp : hshT) if (mp[tmp]) return true; return false;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> (s + 1) >> (t + 1); n = strlen(s + 1), m = strlen(t + 1); build(); int l = 1, r = min(n, m), ans = 0; while (l <= r) { int mid = l + r >> 1; if (check(mid)) l = mid + 1, ans = mid; else r = mid - 1; } cout << ans << '\n'; return 0;}```
## C - Prefix Function Queries
KMP 自动机是弱化版的 AC 自动机,相当于单串的 AC 自动机。先建一个 fail 树,设 $nxt_{i,j}$ 表示从 $i$ 开始沿着 fail 树向上跳得到的最大的位置 $k$ 使 $s_{k+1}=j$ 。
注意到此时我们可以把 KMP 求的过程表示为:对于 $i$ ,从 $fail_{i−1}$ 开始沿着 fail 树向上跳找到的最大的位置 $k$ 使 $s_{k+1}=s_i$ ,若不存在则为 $0$ 。这也可以让我们得到 $fail_i=nxt_{fail_{i−1,s_i}}$ 。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int maxn = 1e6 + 15;int n, m, q, fail[maxn], nxt[maxn][26];char s[maxn];void build(){ int n = strlen(&s[1]); for (int i = 2, j = 0; i <= n; i++) { while (j >= 1 && s[i] != s[j + 1]) j = fail[j]; if (s[i] == s[j + 1]) j++; fail[i] = j; } for (int i = 1; i <= n; i++) { for (int j = 0; j < 26; j++) { nxt[i - 1][j] = nxt[fail[i - 1]][j]; } nxt[i - 1][s[i] - 'a'] = i - 1; } return;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> &s[1] >> q; n = strlen(&s[1]); build(); for (int i = 1; i <= q; i++) { cin >> &s[n + 1]; m = n + strlen(&s[n + 1]); for (int j = n + 1, k = fail[n]; j <= m; j++) { k = nxt[k][s[j] - 'a']; if (s[j] == s[k + 1]) k++; fail[j] = k; cout << k << ' '; for (int c = 0; c < 26; c++) { nxt[j - 1][c] = nxt[fail[j - 1]][c]; } nxt[j - 1][s[j] - 'a'] = j - 1; } cout << '\n'; } return 0;}```
## D - 摩卡串 / string
## E - 失配树
给定字符串 $s$ 和 $m$ 次询问,每次询问求 $s$ 长为 $q,p$ 的前缀的最长公共 border 。
首先跑一遍 KMP 得到 $nxt$ 数组,则长为 $i$ 的前缀的 border 长度为 $nxt_i,nxt_{nxt_i}...$ 。考虑以 $i$ 到 $nxt_i$ 建树,这就是 AC 自动机中只有一个字符串的 fail 树,在树上跑 LCA 即为答案。值得注意的是一个子串的 border 一定小于子串长度。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int maxn = 1e6 + 5;int n, m, p, q, dep[maxn], nxt[maxn][20];char s[maxn];signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> (s + 1); n = strlen(s + 1); int len = 0; for (int i = 2; i <= n; i++) { while (len && s[i] != s[len + 1]) len = nxt[len][0]; if (s[len + 1] == s[i]) len++; nxt[i][0] = len; } for (int j = 1; j < 20; j++) { for (int i = 1; i <= n; i++) { nxt[i][j] = nxt[nxt[i][j - 1]][j - 1]; } } for (int i = 1; i <= n; i++) { int u = i; for (int j = 19; j >= 0; j--) { if (nxt[u][j]) { u = nxt[u][j]; dep[i] += (1 << j); } } } cin >> m; while (m--) { cin >> p >> q; if (dep[p] < dep[q]) swap(p, q); for (int i = 19; i >= 0; i--) { if (dep[nxt[p][i]] >= dep[q]) { p = nxt[p][i]; } } if (p == q) { cout << nxt[p][0] << '\n'; continue; } for (int i = 19; i >= 0; i--) { if (nxt[p][i] != nxt[q][i]) { p = nxt[p][i]; q = nxt[q][i]; } } cout << nxt[p][0] << '\n'; } return 0;}```
## F - 葫芦和斌斌的字符串2
给定字符串 $s$ 有两种操作:
1. 在字符串后添加一个字符。2. 找到字符串最长的出现 $k$ 次的 border 。
给定 $s$ 和 $q$ 次操作,操作一是在 $s$ 尾部加入一个新字符,操作二是求 $s$ 的最长满足在 $s$ 中出现至少 $k$ 次的 border 。
首先建 border 树,发现从 $n$ 往上跳,跳到第一个子树大小 $\ge k$ 的节点即为答案。动态维护很棘手,于是离线下来后树剖+树状数组搞过去。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int maxn = 2e6 + 5;int n, q, len, op[maxn], k[maxn], N[maxn], dep[maxn], nxt[maxn][20], t[maxn], siz[maxn], son[maxn], top[maxn], ide[maxn], tot;vector <int> G[maxn];char s[maxn];void insert(int i) { while (len && s[i] != s[len + 1]) len = nxt[len][0]; if (s[len + 1] == s[i] && i != 1) len++; nxt[i][0] = len, G[len].push_back(i); for (int j = 1; j < 20; j++) { nxt[i][j] = nxt[nxt[i][j - 1]][j - 1]; }}void makedep(int i) { int u = i; for (int j = 19; j >= 0; j--) { if (nxt[u][j]) { u = nxt[u][j]; dep[i] += (1 << j); } } return;}void add(int x, int y) { for (; x <= N[q]; x += x & -x) t[x] += y; }int sum(int x, int y = 0) { for (; x; x -= x & -x) y += t[x]; return y; }int upd(int l, int r, int x) { add(l, x), add(r + 1, -x); }void dfs1(int u, int fa) { siz[u] = 1; for (auto g : G[u]) { dfs1(g, u); siz[u] += siz[g]; if (siz[g] > siz[son[u]]) son[u] = g; }}void dfs2(int u, int T) { top[u] = T, ide[u] = ++tot; if (son[u]) dfs2(son[u], T); for (auto g : G[u]) { if (g == son[u]) continue; dfs2(g, g); }}int dfs3(int u, int fa) { if (u > n) return 0; int sz = 1; for (auto g : G[u]) { sz += dfs3(g, u); } upd(ide[u], ide[u], sz); return sz;}void update(int u) { while (top[u]) { upd(ide[top[u]], ide[u], 1); u = nxt[top[u]][0]; } upd(ide[top[u]], ide[u], 1);}int query(int u, int x) { for (int i = 19; i >= 0; i--) { if (sum(ide[nxt[u][i]]) < x) { u = nxt[u][i]; } } return nxt[u][0];}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> q >> (s + 1); N[0] = n; for (int i = 1; i <= q; i++) { cin >> op[i]; N[i] = N[i - 1]; if (op[i] == 1) { cin >> s[++N[i]]; } else { cin >> k[i]; } } for (int i = 1; i <= N[q]; i++) insert(i); for (int i = 1; i <= N[q]; i++) makedep(i); dfs1(0, -1), dfs2(0, 0), dfs3(0, -1); for (int i = 1; i <= q; i++) { if (op[i] == 1) { update(N[i]); } else { int ans = query(N[i], k[i]); if (ans) cout << ans << '\n'; else cout << "-1\n"; } } return 0;}```
## G - OKR-Periods of Words
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int maxn = 2e6 + 5;int n, f[maxn], len;char a[maxn];signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> (a + 1); for (int i = 2; i <= n; i++) { while (len && a[len + 1] != a[i]) len = f[len]; if (a[len + 1] == a[i]) len++; f[i] = len; } int ans = 0; for (int i = 2; i <= n; i++) { len = i; while (f[len]) len = f[len]; if (f[i]) f[i] = len; ans += i - len; } cout << ans << '\n'; return 0;}```
## H - SZA-Template
问 $s$ 可以被一个子串重叠组成,求该子串长度最大值。
先做一遍 kmp 是肯定的,因为答案一定是 $s$ 的 border 。设 $f_i$ 为前 $i$ 个的答案,若 $f_i=f_{border_i}$ 当且仅当存在 $j$ 使得 $f_j=f_{border_i}$ 且 $i-border_i\le j$ ,否则 $f_i=i$ 。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int maxn = 5e5 + 5;int n, f[maxn], b[maxn], nxt[maxn];char s[maxn];signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> (s + 1); n = strlen(s + 1); for (int i = 2, len = 0; i <= n; i++) { while (len && s[len + 1] != s[i]) len = nxt[len]; if (s[len + 1] == s[i]) len++; nxt[i] = len; } f[1] = b[1] = 1; for (int i = 2; i <= n; i++) { f[i] = i; if (b[f[nxt[i]]] >= i - nxt[i]) f[i] = f[nxt[i]]; b[f[i]] = i; } cout << f[n] << '\n'; return 0;}```
## I - Anthem of Berland
## J - PRE-Prefixuffix
选取 $s$ 的长为 $L$ 的前后缀,求使前后缀循环同构的最大 $L$ 。
循环同构的前后缀使得字符串为 $ABSBA$ 的形式,设 $s_{1...n}$ 的 border 为 $A$ ,而 $s_{|A|+1,...,n-|A|}$ 的 border 为 $B$ ,答案为 $\max(|A|+|B|)$ 。
首先 $A$ 是好求的,用一次 KMP 就可以将所有 $A$ 求出。求 $B$ 时从中心扩展将所有 $B$ 求出,设 $[i+1,n-i]$ 的 border 长度为 $L$ 则 $[i,n-i+1]$ 的 border 长度不大于直接转移的 $L+2$ ,若不再是 border 则减小 $L$ 判断,总共最多只有 $L$ 次减少。
```cpp#include <bits/stdc++.h>#define int long longusing namespace std;using ull = unsigned long long;const int maxn = 1e6 + 5, base = 131, mod = 1e9 + 7;int n, nxt[maxn], bor[maxn], ans;ull h[maxn], pw[maxn] = {1};char s[maxn];inline int Hash(int l, int r) { return (h[r] - h[l - 1] * pw[r - l + 1] % mod + mod) % mod;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> (s + 1); for (int i = 1; i <= n; i++) { pw[i] = pw[i - 1] * base % mod; h[i] = (h[i - 1] * base % mod + s[i]) % mod; } for (int i = 2, len = 0; i <= n; i++) { while (len && s[len + 1] != s[i]) len = nxt[len]; if (s[len + 1] == s[i]) len++; nxt[i] = len; } for (int i = n >> 1; i; i--) { bor[i] = bor[i + 1] + 2; while (bor[i]) { if ((i + bor[i]) << 1 > n) bor[i]--; else if (Hash(i + 1, i + bor[i]) != Hash(n - i - bor[i] + 1, n - i)) bor[i]--; else break; } } for (int i = nxt[n]; i; i = nxt[i]) if ((i << 1) <= n) ans = max(ans, bor[i] + i); cout << ans << '\n'; return 0;}```
## K - 腊肠披萨
## L - Palindrome Partition
## M - 逆にする関数
## N - Om Nom and Necklace