版本比较
[JOISC 2018] 做题记录
正文
增加 276 行删除 1 行
264 行未改动全部展开26526626726826926927027127261 行未改动334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613264 行未改动全部展开 write(res); pc(0xa); } return 0; }``````
#### [P14371 [JOISC 2018] 航空路线图 / Airline Route Map - 洛谷](https://www.luogu.com.cn/problem/P14371)
61 行未改动 } return 0; }```
#### [P14373 [JOISC 2018] 安全门 / Security Gate - 洛谷](https://www.luogu.com.cn/problem/P14373)
思路有点长,感觉以本人的语文水平写出来会是一坨。直接去看 **[官方题解](https://www2.ioi-jp.org/camp/2018/2018-sp-tasks/day3/security_gate-review.pdf)** 吧,写的很清晰了。
大概就是对每个有贡献的 $S'$ 串按照前后缀分类计入贡献。
```cppconst int maxn = 305, mod = 1e9 + 7;inline void madd(int& x, const int& y) { x+= y; (x >= mod) && (x-= mod); }inline void mdec(int& x, const int& y) { x-= y; (x < 0) && (x-= mod); }int n, c[maxn], ans; char s[maxn];
namespace Sub1{int dp[maxn][maxn]; inline int calc(){ mems(dp, 0); dp[0][0] = 1; rep(i, 0, n - 1) { if (c[i] >= 0) rep(j, 0, n - 1) madd(dp[i + 1][j + 1], dp[i][j]); if (c[i] <= 0) rep(j, 1, n) madd(dp[i + 1][j - 1], dp[i][j]); } return dp[n][0]; }}namespace Sub2{int g1[maxn / 2][maxn][2][maxn]; int g2[maxn][maxn][2][maxn]; int f[maxn][maxn]; inline int calc1(int a, int x){ int res = 0; for (int len = 0; len + 1 <= n; len+= 2) if (c[len] <= 0) madd(res, (ll)g1[x][len][1][0] * f[len + 1][2 * a - 1] % mod); return res; }inline int calc2(int a, int x){ int res = 0; for (int len = 0; len + 1 <= n; len+= 2) if (c[len] <= 0) madd(res, (ll)g1[x][len][1][0] * g2[a + x][len + 1][1][2 * a - 1] % mod); return res; }inline int calc(int a, int x) { return (a <= x) ? calc1(a, x) : calc2(a, x); }void init1(){ mems(g1, 0); rep(x, 0, n / 2 - 1) { g1[x][0][x == 0][0] = 1; rep(i, 0, n - 1) { if (c[i] >= 0) { rep(j, 0, x - 1) madd(g1[x][i + 1][j + 1 == x][j + 1], g1[x][i][0][j]); rep(j, 0, x - 1) madd(g1[x][i + 1][1][j + 1], g1[x][i][1][j]); } if (c[i] <= 0) { rep(j, 1, x) madd(g1[x][i + 1][0][j - 1], g1[x][i][0][j]); rep(j, 1, x) madd(g1[x][i + 1][1][j - 1], g1[x][i][1][j]); } } } }void init2(){ mems(g2, 0); rep(x, 0, n - 1) { g2[x][n][x == 0][0] = 1; fep(i, n - 1, 0) { if (c[i] <= 0) { rep(j, 0, n - 1) madd(g2[x][i][0][j + 1], g2[x][i + 1][0][j]); if (x > 0) madd(g2[x][i][1][x], g2[x][i + 1][0][x - 1]); rep(j, x, min(x * 2, n) - 1) madd(g2[x][i][1][j + 1], g2[x][i + 1][1][j]); } if (c[i] >= 0) { rep(j, 1, n - 1) madd(g2[x][i][0][j - 1], g2[x][i + 1][0][j]); madd(g2[x][i][1][x], g2[x][i + 1][0][x + 1]); rep(j, x + 2, min(x * 2, n - 1)) madd(g2[x][i][1][j - 1], g2[x][i + 1][1][j]); } } }}void init3(){ mems(f, 0); f[n][0] = 1; fep(i, n - 1, 0) { if (c[i] <= 0) rep(j, 0, n - 1) madd(f[i][j + 1], f[i + 1][j]); if (c[i] >= 0) rep(j, 1, n - 1) madd(f[i][j - 1], f[i + 1][j]); }}inline int solve() { init1(); init2(); init3(); int res = 0; rep(a, 1, n / 2) rep(x, 0, n / 2 - a) madd(res, calc(a, x)); return res; }}
namespace Sub3{int g1[maxn / 2][maxn][2][maxn];int g2[maxn / 2][maxn][3][maxn * 2];inline int calc(int a, int x){ int res = 0; for (int len = 0; len + 1 <= n; len+= 2) if (c[len] <= 0) madd(res, (ll)g1[x][len][1][0] * g2[a + x][len + 1][2][a * 2 - 1 + n] % mod); return res; } void init1(){ mems(g1, 0); rep(x, 0, n / 2 - 1) { g1[x][0][x == 0][0] = 1; rep(i, 0, n - 1) { if (c[i] >= 0) { rep(j, 0, x - 1) madd(g1[x][i + 1][j + 1 == x][j + 1], g1[x][i][0][j]); rep(j, 0, x - 1) madd(g1[x][i + 1][1][j + 1], g1[x][i][1][j]); } if (c[i] <= 0) { rep(j, 1, x - 1) madd(g1[x][i + 1][0][j - 1], g1[x][i][0][j]); rep(j, 1, x) madd(g1[x][i + 1][1][j - 1], g1[x][i][1][j]); } } } }void init2(int flg){ mems(g2, 0); rep(x, 0, n / 2 - 1) { g2[x][n][(x + flg) == 0][n] = 1; fep(i, n - 1, 0) { if (c[i] <= 0) { rep(j, 0, n - 1) madd(g2[x][i][(j + 1) == (x + flg)][j + 1 + n], g2[x][i + 1][0][j + n]); rep(j, 0, n - 1) madd(g2[x][i][1][j + 1 + n], g2[x][i + 1][1][j + n]); rep(j, -n, min(x * 2, n) - 1) madd(g2[x][i][2][j + n + 1], g2[x][i + 1][2][j + n]); } if (c[i] >= 0) { rep(j, 1, n - 1) madd(g2[x][i][0][j + n - 1], g2[x][i + 1][0][j + n]); rep(j, 0, n - 1) madd(g2[x][i][(j - 1) == -1 ? 2 : 1][j - 1 + n], g2[x][i + 1][1][j + n]); rep(j, -n + 1, min(x * 2, n - 1)) madd(g2[x][i][2][j + n - 1], g2[x][i + 1][2][j + n]); } } }}inline int solve(){ int res = 0; rep(flg, 0, 1) { init1(); init2(flg); rep(a, -n / 2 + 1, n / 2 - 1) rep(x, max(0, -a), n / 2 - abs(a) - 1) madd(res, calc(a, x)); reverse(c, c + n); rep(i, 0, n - 1) c[i]*= -1; } return res; }}
int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; if (n & 1) return puts("0"), 0; rep(i, 0, n - 1) c[i] = s[i] == '(' ? 1 : s[i] == ')' ? -1 : 0; madd(ans, Sub1::calc()); rep(o, 0, 1) { madd(ans, Sub2::solve()); reverse(c, c + n); rep(i, 0, n - 1) c[i]*= -1; } madd(ans, Sub3::solve()); write(ans); pc(0xa); return 0; }```
#### [P14374 [JOISC 2018] 糖果 / Candies - 洛谷](https://www.luogu.com.cn/problem/P14374)
(反悔贪心裸题?)
直接链表+堆维护,每次取最大的,取 $i$ 后 $a_i\leftarrow a_{\text{pre}}+a_{\text{nxt}}-a_i$ 并删去 $\text{pre}$ 和 $\text{nxt}$。
关于边界情况可以直接给 $0$ 和 $n+1$ 的位置挂上 $-\infty$,以规避讨论。
不知道为什么评紫。
```cppconst int maxn = 2e5 + 5; const ll inf = 1e18;int n, l[maxn], r[maxn], vis[maxn], cnt; ll a[maxn], ans;
priority_queue<pli> pq;
int main(){ read(n); rep(i, 1, n) read(a[i]); rep(i, 1, n) l[i] = i - 1, r[i] = i + 1; rep(i, 1, n) pq.push({a[i], i}); a[0] = a[n + 1] = -inf; while (cnt < (n + 1) / 2) { int p = pq.top().sec; pq.pop(); if (vis[p]) continue; ans+= a[p]; a[p] = a[l[p]] + a[r[p]] - a[p]; vis[l[p]] = vis[r[p]] = 1; r[l[l[p]]] = p; l[r[r[p]]] = p; l[p] = l[l[p]]; r[p] = r[r[p]]; pq.push({a[p], p}); write(ans); pc(0xa); ++cnt; } return 0; }```
#### [P14375 [JOISC 2018] 图书馆 / Library - 洛谷](https://www.luogu.com.cn/problem/P14375)
$n\le 1000,q\le 2\times 10^4$,因而猜测大概是 $O(n\log n)$ 的算法。
发现对一个集合 $S$ 进行询问返回的是 $|S|-w(S)$,其中 $w(S)$ 表示 $S$ 中在图书馆里相邻的图书对数。
于是假设先询问 $S$,再询问 $S\cup \{x\}$,其中 $x$ 为一个不在 $S$ 的元素,那么可以得出 $x$ 是否与 $S$ 中的元素相邻。
考虑对于每个 $x$,通过二分 $S$ 确定与 $x$ 相邻的元素。由于一个元素最多有两个相邻元素,因此要二分两次。
初始时可以直接通过 $n$ 次询问确定排列的一个端点。
总次数 $O(n+Cn\log n),C=2$。
```cppint Query(const vector<int>& S);void Answer(const vector<int>& S);
const int maxn = 1005; bool vis[maxn];
void Solve(int n){ if (n == 1) return Answer({1}), void(); vector<int> q(n), ans(n); rep(i, 1, n) { for (int& i : q) i = 1; q[i - 1] = 0; if (Query(q) == 1) { vis[i] = 1, ans[0] = i; break; } } rep(o, 1, n - 1) { vector<int> a; rep(i, 1, n) if (!vis[i]) a.pbk(i); while (a.size() != 1) { vector<int> b, c; int cnt = 0; for (int i : a) { if (cnt == a.size() / 2) c.pbk(i); else ++cnt, b.pbk(i); } for (int& i : q) i = 0; for (int i : b) q[i - 1] = 1; int x = Query(q); for (int& i : q) i = 0; for (int i : b) q[i - 1] = 1; q[ans[o - 1] - 1] = 1; (Query(q) == x) ? swap(a, b) : swap(a, c); } ans[o] = a[0]; vis[ans[o]] = 1; } Answer(ans);}```
#### [P14376 [JOISC 2018] 野猪 / Wild Boar - 洛谷](https://www.luogu.com.cn/problem/P14376)
咕咕咕~