正文
增加 214 行删除 1 行
10123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214## 目录- [T1](/problem/U647590)- [T2](/problem/U647591)- [T3](/problem/U647592)- [T4](/problem/U647593)## T1 英语听写 / Writing Qualification### 思路状态机。### 代码```cpp#include <bits/stdc++.h>using namespace std;string s; int N, cnt;int main(){cin >> N >> s; cnt = 0;int st[5] = {0};for(char ch : s)if(ch == 'c') st[0]++;else if(ch == 'o' && st[0] > 0) st[0]--, st[1]++;else if(ch == 'u' && st[1] > 0) st[1]--, st[2]++;else if(ch == 'n' && st[2] > 0) st[2]--, st[3]++;else if(ch == 't' && st[3] > 0) st[3]--, st[4]++;cnt = st[4];cout << cnt << endl;return 0;}```[提交记录](/record/256516728)。## T2 连续机惨 / Rapid Operation### 思路区间 DP。~~似乎和[最大子段和](/problem/P1115)只差一个 $k$。~~设 $dp_{i, j, 0/1}$ 表示前 $i$ 台电脑,选择了 $j$ 段,是否机惨第 $i$ 台电脑的最大和。#### 状态转移不选 $a_i$:- 前一个也没选:$dp_{i-1, j, 0}$。- 前一个选了:$dp_{i-1, j, 1}$。选 $a_i$:- 前一个也选了:$a_i$ 和 $a_{i - 1}$ 属于同一段。- 前一个没选:$a_i$ 是新的一段开始。### 代码```cpp#include <bits/stdc++.h>using namespace std;const int inf = 0x3f3f3f3f;int n, k, a[100005], dp[100005][55][2], cnt;int main(){cin >> n >> k;for(int i=1; i<=n; i++) cin >> a[i];memset(dp, -inf, sizeof(dp));for(int i=0; i<=n; i++) dp[i][0][0] = 0;for(int i=1; i<=n; i++)for(int j=0; j<=k; j++){dp[i][j][0] = max(dp[i-1][j][0], dp[i-1][j][1]);if(j > 0) dp[i][j][1] = max(dp[i-1][j][1], dp[i-1][j-1][0]) + a[i];}cnt = 0;for(int j=0; j<=k; j++)cnt = max(cnt, max(dp[n][j][0], dp[n][j][1]));cout << cnt << endl;return 0;}```[提交记录](/record/256903548)。## T3 随机做题 / Random Examination### 思路概率 DP。注意到时间限制。注意到模数。定义 $dp_{i, j}$ 为做到第 $i$ 道题后总分对 $m$ 的余数为 $j$ 的概率。如果第 $i$ 道题做错了,则得分与上次相同,$dp_{i, j} = dp_{i-1, j} \times (1 - p_i)$。如果第 $i$ 道题做对了,则获得 $a_i$ 的分数,上一次得分为 $j - a_i$,$dp_{i, j} = dp_{i-1, j-a_i} \times p_i$。故 $dp_{i,j} = dp_{i-1, j} \times (1 - p_i) + dp_{i-1, j-a_i} \times p_i$。如果直接开 `dp[100100][1010]` 显然会 MLE,所以使用滚动数组。最终状态转移方程为 $dp_{i,j} = dp_{i \oplus1, j} \times (1-p_i) + dp_{i \oplus1, (j - a_i + m) \bmod m} \times p_i$。### 代码```cpp#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e5 + 10;const int mod = 998244853;ll n, m, a[N], p[N], dp[N][2], tot;int main(){ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);cin >> n >> m;for(int i=1; i<=n; i++) {cin >> a[i]; a[i] %= m;}for(int i=1; i<=n; i++) cin >> p[i];dp[0][0] = 1; //还没做题时总分对m的余数为0的概率为1for(int i=1; i<=n; i++){tot ^= 1; //if(tot=0) tot=1; else tot=0;for(int j=0; j<m; j++) dp[j][tot] = 0;for(int j=0; j<m; j++)(dp[j][tot] += (dp[j][tot^1] * (1-p[i] + mod) % mod + dp[(j-a[i] + m) % m][tot^1] * p[i] % mod)) %= mod;}cout << dp[0][tot];return 0;}```[提交记录](/record/256900804)。## T4 迷宫连通 / Tree Renovation### 题意wsb 的迷宫里有三种格子:- `.` 免费通行。- 非 tree 的 `#` 无法通行。- 是 tree 可以花费 $c_i$ 变成可通行。### 思路将每个格子视为图中节点,从当前格子向四个方向扩展:- 若相邻格子是 `.`,边权设置 $0$。- 相邻格子是 tree,边权为 $c_i$。- 相邻格子是普通墙,**不连边**(跳过)。注意到所有边权均 $\geqslant 0$,考虑带权 Dijkstra。### 复杂度分析时间复杂度:$O(nm \log(nm))$。空间复杂度:$O(nm)$。### 代码```cpp#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef pair<ll, pair<int, int>> plp; //{cost, {x, y}}const ll inf = 1e18;const int N = 510;int dx[5] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};int n, m, k;ll tr[N][N], dis[N][N];string s[N];int main(){ios::sync_with_stdio(false); cin.tie(nullptr);cin >> n >> m >> k;for(int i=1; i<=n; i++) cin >> s[i], s[i] = " " + s[i];for(int i=1, x, y, c; i<=k; i++){cin >> x >> y >> c;tr[x][y] = c;}for(int i=1; i<=n; i++)for(int j=1; j<=m; j++)dis[i][j] = inf;priority_queue<plp, vector<plp>, greater<plp>> q;dis[1][1] = 0;q.push({0, {1, 1}});while(!q.empty()){auto [c, xy] = q.top(); q.pop();int x = xy.first, y = xy.second;if(c != dis[x][y]) continue;for(int d=0; d<4; d++) //尝试行走{int nx = x + dx[d], ny = y + dy[d]; ll nc = c;if(nx < 1 || nx > n || ny < 1 || ny > m) continue;if(tr[nx][ny] > 0) nc += tr[nx][ny]; //treeelse if(s[nx][ny] == '#') continue; //墙if(nc < dis[nx][ny]) //最小代价{dis[nx][ny] = nc;q.push({nc, {nx, ny}});}}}if(dis[n][m] == inf) puts("-1");else cout << dis[n][m] << '\n';return 0;}```[提交记录](/record/257151088)。