版本比较
记录 续
正文
增加 709 行删除 0 行
1,021 行未改动全部展开102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341,021 行未改动全部展开 cout << ans << '\n'; }}```
### ABC403G不想打平衡树,于是直接分块,每个块长度超过 $2\sqrt n$ 就直接重构,时间复杂度 $O(n\sqrt n)$。
每次插入时,如果直接暴力 sort 的话会带 $\log$,直接炸掉,所以要写归并之类的。
```cpp#include <bits/stdc++.h>using namespace std;#define int long longtemplate<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }constexpr int N = 3e5 + 9, V = 1e9, S = 548;int Q, n, a[N / S + 9][2 * S + 9], cnt[N / S + 9], bel[N], mxb, st[N / S + 9], ed[N / S + 9], b[N], su[N / S + 9][2];void rebuild() { int c = 0; for (int i = 1; i <= mxb; ++i) { for (int j = 1; j <= cnt[i]; ++j) { b[++c] = a[i][j]; } su[i][0] = su[i][1] = 0; cnt[i] = 0; } assert(n == c); for (int i = 1; i <= n; ++i) { bel[i] = (i - 1) / S + 1; a[bel[i]][++cnt[bel[i]]] = b[i]; su[bel[i]][i & 1] += b[i]; } mxb = bel[n];}void insert(int x, int y) { a[x][++cnt[x]] = y; inplace_merge(a[x] + 1, a[x] + cnt[x], a[x] + cnt[x] + 1); bel[++n] = x, su[x][0] = su[x][1] = 0; int sz = 0; for (int i = 1; i < x; ++i) sz += cnt[i]; for (int j = 1; j <= cnt[x]; ++j) su[x][(sz + j) % 2] += a[x][j]; for (int i = x + 1; i <= mxb; ++i) { swap(su[i][0], su[i][1]); } if (cnt[x] > 2 * S) rebuild();}signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> Q; mxb = 1; int z = 0; while (Q--) { int y, x; cin >> y; x = (y + z) % V + 1; int cur = 1; while (cur < mxb && a[cur][cnt[cur]] < x) ++cur; insert(cur, x); int ans = 0, be = 0; for (int i = 1; i <= mxb; ++i) ans += su[i][1]; cout << (z = ans) << '\n'; }}```
### 24 点(midnight)模拟赛题,很可做啊。
首先显然除了 $1$ 以外的所有数都会使答案变大,所以我们先统计所有大于 $1$ 的点权之和,如果大于 $z$ 那么必然无解。
接下来不考虑以上这种无解的情况。
然后考虑 $1$,由于 $m \le 30$ 所以此时长度超过 $m$ 的必然有解,可以通过把一些 $\times 1$ 改成 $+1$ 来实现。
于是所有长度 $\ge m$ 的都有解。
那么对于长度 $\le m$ 的,我们可以直接暴力 $O(qm^2)$。
总时间复杂度 $O(q(\log n + m^2))$,$q \le 10^6, m \le 30$ 由于给了 $4$ s,所以冲过去了。
可以继续优化,$m \le 30$,而且状态是 $0/1$,所以直接拿 int 状压,然后加很简单,乘的话,我们发现 $\times 1$ 是简单的,对于 $\ge 2$ 的情况,只有 $1 \sim \lfloor m2 \rfloor$ 是有用的,于是我们可以直接预处理出这些状态对于不同的乘数的转移,然后就可以去掉一个 $m$。
时间复杂度 $O(m^22^{\frac m2} + q(\log n + m))$
```cpp#include <bits/stdc++.h>using namespace std;#define int long longtemplate<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }constexpr int N = 1e6 + 9;int n, m, q, a[N], fa[N], dep[N], dfn[N], idx, mi[20][N], sz[N], c[N], c1[N];int h[1 << 16][31];bool vis[N];vector<int> es[N];void update1(int x, int y) { for (; x <= n; x += x & -x) c1[x] += y; }int query1(int x, int r = 0) { for (; x > 0; x -= x & -x) r += c1[x]; return r; }void update(int x, int y) { for (; x <= n; x += x & -x) c[x] += y; }int query(int x, int r = 0) { for (; x > 0; x -= x & -x) r += c[x]; return r; }int dfs(int x, int ff, int d = 0, int b = 0) { fa[x] = ff, dep[x] = dep[ff] + 1, mi[0][dfn[x] = ++idx] = ff; b += a[x] > 1 ? a[x] : 0, d += a[x]; update(dfn[x], d), update(dfn[x] + 1, -d); update1(dfn[x], b), update1(dfn[x] + 1, -b); for (int y : es[x]) if (y ^ ff) sz[x] += dfs(y, x, d, b); return ++sz[x];}int get(int x, int y) { return dfn[x] < dfn[y] ? x : y; }int lca(int x, int y) { if (x == y) return x; if ((x = dfn[x]) > (y = dfn[y])) swap(x, y); int d = __lg(y - ++x + 1); return get(mi[d][x], mi[d][y - (1 << d) + 1]);}int query(int x, int y, int z) { int f = lca(x, y); int d = query(dfn[x]) + query(dfn[y]) - query(dfn[f]) - query(dfn[fa[f]]); int d1 = query1(dfn[x]) + query1(dfn[y]) - query1(dfn[f]) - query1(dfn[fa[f]]); // cerr << x << ' ' << y << ' ' << z << ' ' << f << '\n'; // cerr << "d: " << d << ' ' << "d1: " << d1 << '\n'; if (d1 <= z && z <= d) return 1; if (d1 > z) return 0; return -1;}signed main() {#ifndef LOCAL freopen("midnight.in", "r", stdin); freopen("midnight.out", "w", stdout);#endif cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> m >> q; for (int i = 0; i < 1 << 16; ++i) { for (int j = 2; j <= m; ++j) { for (int k = 0; k * j <= m; ++k) { if ((i >> k & 1) && k * j <= m) h[i][j] |= 1 << (k * j); } } } for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i < n; ++i) { int x, y; cin >> x >> y; es[x].push_back(y), es[y].push_back(x); } dfs(1, 0); for (int i = 1; i < 20; ++i) { for (int j = 1; j <= n; ++j) mi[i][j] = get(mi[i - 1][j], mi[i - 1][j + (1 << (i - 1))]); } while (q--) { int o, x, y, z; cin >> o >> x >> y; if (o == 1) update(dfn[x], y - a[x]), update(dfn[x] + sz[x], a[x] - y), update1(dfn[x], (y > 1 ? y : 0) - (a[x] > 1 ? a[x] : 0)), update1(dfn[x] + sz[x], (a[x] > 1 ? a[x] : 0) - (y > 1 ? y : 0)), a[x] = y; else { cin >> z; if (int res = query(x, y, z); ~res) { cout << res << '\n'; continue; } bool ch = 0; if (dep[x] < dep[y]) swap(x, y), ch = 1; vector<int> ls, rs; int ffa = 0, tx = x, ty = y; while (dep[x] > dep[y]) { x = fa[x]; } if (x == y) ffa = x; while (fa[x] != fa[y]) { x = fa[x], y = fa[y]; } if (x != y) ffa = fa[x]; x = tx, y = ty; if (ch) swap(x, y); for (; x != ffa; x = fa[x]) ls.push_back(x); for (; y != ffa; y = fa[y]) rs.push_back(y); ls.push_back(ffa); vector<int> vec = ls; reverse(rs.begin(), rs.end()); for (int i : rs) vec.push_back(i); int f = 1 << a[vec[0]]; for (size_t i = 1; i < vec.size(); ++i) { int g = f; f = 0; if (a[vec[i]] == 1) f = g; f |= g << a[vec[i]]; f |= h[g & ((1 << 16) - 1)][a[vec[i]]]; } // cerr << '\n'; cout << (f >> z & 1) << '\n'; } }}```
-----------
2025.10.31
### P4269
对 $s_i$ 做扫描线,然后把 $> s_i$ 的位置删掉,剩下的点之间的相邻间隔超过 $d_i$ 就不可以,链表维护一下就可以了。
$O(n \log n)$,瓶颈在排序。
```cpp#include <bits/stdc++.h>using namespace std;template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }constexpr int N = 1e5 + 9;int n, m, mx, a[N], o[N], ans[N], nxt[N], pre[N];struct shoes { int s, d, i;} b[N];void del(int x) { cmax(mx, nxt[x] - pre[x]); nxt[pre[x]] = nxt[x], pre[nxt[x]] = pre[x];}signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i], o[i] = i, nxt[i] = i + 1, pre[i] = i - 1; sort(o + 1, o + n + 1, [&](int x, int y) -> bool { return a[x] > a[y]; }); for (int i = 1; i <= m; ++i) cin >> b[i].s >> b[i].d, b[i].i = i; sort(b + 1, b + m + 1, [&](auto x, auto y) -> bool { return x.s > y.s; }); for (int i = 1, j = 0; i <= m; ++i) { while (j < n && a[o[j + 1]] > b[i].s) del(o[++j]); ans[b[i].i] = b[i].d >= mx; } for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';}```
### CF985F同构等价于对于每个字母 $c$ 存在一个字母 $c'$,他们出现的位置相同。
于是直接哈希记录每个字母的出现,然后把所有字母都算一遍最后判一下集合是否相等即可。
$O(n|\sum|)$。
```cpp#include <bits/stdc++.h>using namespace std;template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }#define int long longusing u64 = uint64_t;struct hash_t { static constexpr u64 md = (1LL << 61) - 1; u64 h; constexpr hash_t() : h(0) {} constexpr hash_t(u64 v) : h(v % md) {} hash_t operator+(const hash_t &x) const { return (h + x.h) >= md ? h + x.h - md : h + x.h; } hash_t operator-(const hash_t &x) const { return h >= x.h ? h - x.h : h + md - x.h; } hash_t operator*(const hash_t &x) const { __int128 tmp = h; tmp *= x.h; u64 r = u64(tmp & md) + u64(tmp >> 61); return r >= md ? r - md : r; }};constexpr int N = 2e5 + 9;constexpr hash_t seed = 131ULL;constexpr u64 msk = 131881937;int n, m, p[26][N], cnt[26];hash_t h[26][N], pw[N];string s;u64 shift(u64 x) { return x ^= msk, x ^= x << 5, x ^= x >> 11, x ^= x << 54, x ^= msk;}hash_t get(int q, int l, int r) { return h[q][r] - h[q][l - 1] * pw[r - l + 1]; }signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> m; cin >> s, s = ' ' + s; pw[0] = 1; for (int i = 1; i <= n; ++i) pw[i] = pw[i - 1] * seed; for (int i = 0; i < 26; ++i) { for (int j = 1; j <= n; ++j) h[i][j] = h[i][j - 1] * seed + shift(s[j] - 'a' == i); } while (m--) { int x, y, len; cin >> x >> y >> len; vector<u64> ls, rs; for (int i = 0; i < 26; ++i) ls.push_back(get(i, x, x + len - 1).h); for (int i = 0; i < 26; ++i) rs.push_back(get(i, y, y + len - 1).h); sort(ls.begin(), ls.end()), sort(rs.begin(), rs.end()); if (ls == rs) cout << "YES\n"; else cout << "NO\n"; }}```
### P4302区间 dp,如果当前区间可以折叠就取最小即可。
$O(n^4)$。
```cpp#include <bits/stdc++.h>using namespace std;template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }constexpr int N = 109;int f[N][N], n;string s;int dfs(int l, int r) { if (f[l][r]) return f[l][r]; auto &res = f[l][r] = r - l + 1; for (int k = l; k < r; ++k) cmin(res, dfs(l, k) + dfs(k + 1, r)); for (int k = 1; k < r - l + 1; ++k) { if ((r - l + 1) % k) continue; bool ok = true; for (int j = l; j <= r; ++j) { ok &= s[j] == s[(j - l) % k + l]; } if (ok) cmin(res, dfs(l, l + k - 1) + 3 + (int)(log10((r - l + 1) / k))); } return res;}signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> s, n = s.size(), s = ' ' + s; cout << dfs(1, n);}```
### P1654考虑 $E((x + 1)^3) = E(x^3) + E(3x^2) + E(3x) + 1$,$E((x + 1)^2) = E(x^2) + E(2x) + 1$,$E(x + 1) = E(x) + 1$,于是我们可以维护 $E(x), E(x^2), E(x^3)$,直接递推即可。
$O(n)$。
```cpp#include <bits/stdc++.h>using namespace std;template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }#define d64 long doubleconstexpr int N = 1e5 + 9;int n;d64 p, f[N], x1[N], x2[N];signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) { cin >> p; x1[i] = (x1[i - 1] + 1) * p, x2[i] = (x2[i - 1] + 2 * x1[i - 1] + 1) * p; f[i] = f[i - 1] + (3 * x2[i - 1] + 3 * x1[i - 1] + 1) * p; } cout << fixed << setprecision(1) << f[n];}```
### P5839首先用 floyd 求出每个字符变化的最小花费。
然后考虑 dp,$f_i$ 表示 $i$ 结尾的答案,转移很简单直接枚举一段 $[j + 1, i]$ 以及要把它变成的颜色 $c$,$f_i = \min f_j + w(j + 1, i, c)$,$w(j + 1, i, c)$ 表示这一段的代价,用前缀和算一下即可。
容易发现 $w(l, r, c) = s_{r, c} - s_{l - 1, c}$,于是转移即为 $f_i = \min f_j + s_{i, c} - s_{j, c}$,我们令 $g_{i, c} = \min\limits_{j \le i} f_j - s_{j, c}$,简单前缀和就做完了。
$O(nm + m^3)$。
```cpp#include <bits/stdc++.h>using namespace std;template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }constexpr int N = 1e5 + 9;int n, m, k, a[26][26], f[N], g[N][26], su[N][26];string s;signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> m >> k >> s; s = ' ' + s; for (int i = 0; i < m; ++i) for (int j = 0; j < m; ++j) cin >> a[i][j]; for (int i = 0; i < m; ++i) for (int j = 0; j < m; ++j) for (int k = 0; k < m; ++k) cmin(a[j][k], a[j][i] + a[i][k]); for (int i = 1; i <= n; ++i) for (int j = 0; j < m; ++j) su[i][j] = su[i - 1][j] + a[s[i] - 'a'][j]; memset(f, 0x3f, sizeof f); memset(g, 0x3f, sizeof g); f[0] = 0; for (int i = 0; i <= n; ++i) { for (int l = 0; l < m; ++l) { if (i >= k) cmin(f[i], su[i][l] + g[i - k][l]); } for (int j = 0; j < m; ++j) g[i][j] = min(g[i - 1][j], f[i] - su[i][j]); } cout << f[n];}```
### NewMoneySystem(TopCoder)好神秘。
首先,假设第 $i$ 张纸币是 $x$ 元,那么第 $i + 1$ 张是 $qx$($q \in \{2, 3, 4, 5\}$)。如果我们用了 $p > q$ 张 $i$,就可以换成 $\lfloor \dfrac pq \rfloor$ 张 $i + 1$,于是我们考虑记忆化搜索。
$f_{x, y}$ 表示现在要用 $x$ 张,还有 $y$ 种面值的答案。
那么初始时,我们全部用 $1$,要 $n$ 张,也就是 $f_{n, k - 1}$。
转移考虑本次 $q$,$f_{x, y} = \min\limits_q f_{\lfloor\frac xq\rfloor, y - 1} + x \bmod q$。
时间复杂度不太懂,不过由于每次 $n$ 至少减半,可能是 $O(k\log^4 n)$ 的?
```cpp#include <bits/stdc++.h>using namespace std;template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }#define int long longint n, k;map<pair<int, int>, int> mp;int dfs(int x, int y) { if (mp.count({x, y})) return mp[{x, y}]; if (y == 0) return x; int res = 0x3f3f3f3f3f3f3f3f; for (int i = 2; i <= 5; ++i) cmin(res, dfs(x / i, y - 1) + x % i); return mp[{x, y}] = res;}signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> k; cout << dfs(n, k - 1);}```
### P2051首先注意到每一行每一列最多放两个,然后开始 dp。
$f_{i, j, k}$ 表示前 $i$ 行,$j$ 列放了 $1$ 个,$k$ 列放了 $2$ 个,$m - j - k$ 列没放。
转移考虑分类讨论这一行放几个放在那几列即可。
细节不多,$O(n^3)$。
```cpp#include <bits/stdc++.h>using namespace std;template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }constexpr int N = 109, md = 9999973;#define int long longint n, m, f[N][N][N];void update(int &x, int y) { x += y; x >= md ? x -= md : 0; }int choose(int x) { return (x * (x - 1) / 2) % md; }signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> m; f[0][0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j <= m; ++j) { for (int k = 0; k + j <= m; ++k) { update(f[i + 1][j][k], f[i][j][k]); // none update(f[i + 1][j + 1][k], f[i][j][k] * (m - j - k) % md); // one in empty if (j > 0) update(f[i + 1][j - 1][k + 1], f[i][j][k] * j % md); // one in one update(f[i + 1][j + 2][k], f[i][j][k] * choose(m - j - k) % md); // two in empty update(f[i + 1][j][k + 1], f[i][j][k] * j % md * (m - j - k) % md); // one in one, one in empty if (j > 1) update(f[i + 1][j - 2][k + 2], f[i][j][k] * choose(j) % md); // two in one } } } int ans = 0; for (int i = 0; i <= m; ++i) for (int j = 0; j + i <= m; ++j) update(ans, f[n][i][j]); cout << ans;}```
### P4441直接 dp,$f_{i, j, k}$ 表示 $(i, j)$ 时,左括号个数减右括号个数为 $k$ 的最大长度。
输出方案直接用优先队列跑 bfs,这样每次最先到达一个状态的一定是最优的。
$O(r^2c)$
```cpp#include <bits/stdc++.h>using namespace std;template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }constexpr int N = 309;int n, m, f[N][N][N];char s[N][N];bool vis[N][N][N];struct state { string s; int x, y, k; state(string _s = "", int _x = 0, int _y = 0, int _k = 0) : s(_s), x(_x), y(_y), k(_k) {} bool operator<(const state &r) const { if (s.size() < r.s.size()) return 0; if (s.size() > r.s.size()) return 1; return s > r.s; }};signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) cin >> s[i][j]; memset(f, -0x3f, sizeof f); for (int i = 1; i <= m; ++i) { if (s[n][i] == 'M') f[n][i][0] = 0; } for (int i = n - 1; i >= 1; --i) { for (int j = 1; j <= m; ++j) { for (int k = 0; k <= n; ++k) { if (s[i][j] == '(' && k > 0) cmax(f[i][j][k], 1 + max({f[i + 1][j - 1][k - 1], f[i + 1][j + 1][k - 1], f[i + 1][j][k - 1]})); if (s[i][j] == ')') cmax(f[i][j][k], 1 + max({f[i + 1][j - 1][k + 1], f[i + 1][j][k + 1], f[i + 1][j + 1][k + 1]})); if (s[i][j] == '.') cmax(f[i][j][k], max({f[i + 1][j - 1][k], f[i + 1][j][k], f[i + 1][j + 1][k]})); } } } int ans = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) cmax(ans, f[i][j][0]); priority_queue<state> q; vector<string> vec; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (f[i][j][0] == ans) q.push({"", i, j, 0}); while (!q.empty()) { auto [str, x, y, k] = q.top(); q.pop(); if (vis[x][y][k]) continue; vis[x][y][k] = 1; if (s[x][y] == 'M') { vec.push_back(str); continue; } if (s[x][y] == '(' && k > 0) { if (f[x][y][k] == 1 + f[x + 1][y - 1][k - 1]) q.push({s[x][y] + str, x + 1, y - 1, k - 1}); if (f[x][y][k] == 1 + f[x + 1][y + 1][k - 1]) q.push({s[x][y] + str, x + 1, y + 1, k - 1}); if (f[x][y][k] == 1 + f[x + 1][y][k - 1]) q.push({s[x][y] + str, x + 1, y, k - 1}); } if (s[x][y] == ')') { if (f[x][y][k] == 1 + f[x + 1][y - 1][k + 1]) q.push({s[x][y] + str, x + 1, y - 1, k + 1}); if (f[x][y][k] == 1 + f[x + 1][y + 1][k + 1]) q.push({s[x][y] + str, x + 1, y + 1, k + 1}); if (f[x][y][k] == 1 + f[x + 1][y][k + 1]) q.push({s[x][y] + str, x + 1, y, k + 1}); } if (s[x][y] == '.') { if (f[x][y][k] == f[x + 1][y - 1][k]) q.push({str, x + 1, y - 1, k}); if (f[x][y][k] == f[x + 1][y + 1][k]) q.push({str, x + 1, y + 1, k}); if (f[x][y][k] == f[x + 1][y][k]) q.push({str, x + 1, y, k}); } } sort(vec.begin(), vec.end()); cout << ans << "\n" << vec[0];}```
### P2519将 $a, b$ 转化一下,可以得到第 $i$ 个人的排名区间 $[a + 1, n - b]$,然后我们可以类似扫描线,把所有的左端点挂到右端点上,然后 dp,$f_r = \max f_{l - 1} + \min(cnt_{[l, r]}, r - l + 1)$,$cnt$ 表示区间出现的个数。
$O(n)$。
```cpp#include <bits/stdc++.h>using namespace std;template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }constexpr int N = 1e5 + 9;int n, f[N];map<pair<int, int>, int> cnt;vector<int> l[N];signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) { int a, b; cin >> a >> b; int L = a + 1, R = n - b; if (++cnt[{L, R}] == 1) l[R].push_back(L); } for (int i = 1; i <= n; ++i) { f[i] = f[i - 1]; for (int j : l[i]) cmax(f[i], f[j - 1] + min(i - j + 1, cnt[{j, i}])); } cout << n - f[n];}```
### P2943注意到直接分 $n$ 组,答案为 $n$,所以 $K^2 \le n \Rightarrow K \le \sqrt n$。
然后考虑维护一个 $g_{i, j}$ 表示最小的满足 $[g_{i, j}, i]$ 有 $j$ 种不同的。这个可以递推,我们令 $p_i$ 表示最大的满足 $a_{p_i} = a_i$ 的下标,那么如果 $p_i \ge g_{i - 1, j}$ 则 $g_{i, j} = g_{i - 1, j}$,否则 $g_{i, j} = g_{i - 1, j - 1}$,特别地 $g_{i, 0} = i + 1$。
于是 $f_i = \min f_{g_{i, j} - 1} + j^2$,$g$ 可以滚掉一维。
$O(n\sqrt n)$。
```cpp#include <bits/stdc++.h>using namespace std;template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }#define int long longconstexpr int N = 4e4 + 9;int n, m, p[N], f[N], g[N], pre[N], lst[N];set<int> s;signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> p[i], pre[i] = lst[p[i]], lst[p[i]] = i; } memset(f, 0x3f, sizeof f), f[0] = 0; int S = sqrt(n); for (int i = 1; i <= S; ++i) g[i] = n; for (int i = 1; i <= n; ++i) { g[0] = i; for (int j = S; j >= 1; --j) { if (pre[i] >= g[j]) continue; g[j] = g[j - 1]; } for (int j = 1; j <= S; ++j) cmin(f[i], f[g[j] - 1] + j * j); } cout << f[n];}```
### P8476
显然有一个 dp,令 $f_{i, j}$ 表示 $b_i = j$ 的答案,$f_{i, j} = \min\limits_{j \le k} f_{i - 1, k} + f(j, a_i)$。
我们令 $g_{i, j} = \min\limits_{j \le k} f_{i, k}$,那么就成了 $f_{i, j} = g_{i - 1, j} + f(j, a_i)$。
我们考虑把 $f$ 拆开:
1. 若 $j \ge a_i$,则 $f_{i, j} = g_{i - 1, j} + j - a_i$。2. 若 $j < a_i$,则 $f_{i, j} = g_{i - 1, j} + C$。
那么再回来考虑 $g_{i, j}$,容易发现 1、2 两种转移分别具有单调性,于是我们求后缀 min 等价于覆盖一个区间。
具体地,$j \ge a_i$ 时都是单调的,而 $j < a_i$,时,我们可以找到最小的 $p$ 满足 $f_{i, p} \ge f_{i, a_i}$,于是取 min 相当于将 $[p, a_i)$ 赋值为 $f_{i, a_i}$。
综上,每次先分类转移,再求后缀 min,最终答案即为 $g_{n, 1}$。
用线段树维护,$O(n\log n)$。
```cpp#include <bits/stdc++.h>using namespace std;#define int long longtemplate<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }constexpr int N = 1e6 + 9;int n, C, a[N], b[N], sgt[N << 2];struct tag { int as, ad, add; tag(int x = -1, int y = 0, int z = 0) : as(x), ad(y), add(z) {} tag operator+(const tag &x) const { if (x.as != -1) return x; tag z = *this; z.ad += x.ad, z.add += x.add; return z; }} tg[N << 2];void apply(int u, int r, tag z) { if (z.as != -1) sgt[u] = z.as; sgt[u] += z.ad + b[r] * z.add; tg[u] = tg[u] + z;}void down(int u, int l, int r) { int m = (l + r) >> 1; apply(u << 1, m, tg[u]), apply(u << 1 | 1, r, tg[u]); tg[u] = tag();}void update(int u, int l, int r, int x, int y, tag z) { if (x > y) return ; if (x <= l && r <= y) return apply(u, r, z); int m = (l + r) >> 1; down(u, l, r); if (m >= x) update(u << 1, l, m, x, y, z); if (m < y) update(u << 1 | 1, m + 1, r, x, y, z); sgt[u] = max(sgt[u << 1], sgt[u << 1 | 1]);}int query(int u, int l, int r, int x) { if (l == r) return sgt[u]; int m = (l + r) >> 1; down(u, l, r); if (m >= x) return query(u << 1, l, m, x); else return query(u << 1 | 1, m + 1, r, x);}int find(int u, int l, int r, int p, int k) { if (r <= p && sgt[u] < k) return -1; if (l == r) return l; int m = (l + r) >> 1; down(u, l, r); int res = find(u << 1, l, m, p, k); if (~res) return res; if (m < p) res = find(u << 1 | 1, m + 1, r, p, k); return res;}signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> C; for (int i = 1; i <= n; ++i) cin >> a[i], b[i] = a[i]; sort(b + 1, b + n + 1); for (int i = 1; i <= n; ++i) { int p = lower_bound(b + 1, b + n + 1, a[i]) - b; update(1, 1, n, p + 1, n, {-1, -a[i], 1}); if (p == 1) continue; update(1, 1, n, 1, p - 1, {-1, C, 0}); int k = query(1, 1, n, p); int ps = find(1, 1, n, p - 1, k); if (ps != -1) update(1, 1, n, ps, p - 1, {k, 0, 0}); } cout << query(1, 1, n, 1);}```