版本比较
模拟赛四道题题解
标题
增加 1 行删除 1 行
11四道题的题解模拟赛四道题题解正文
增加 93 行删除 11 行
12345678910114 行未改动1617181913202122232456 行未改动8182838478858687888990849192869394888990919596979899100989910110210310411 行未改动1161171181191191201211221231245 行未改动130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214## 目录
- [T1](/problem/U647590)- [T2](/problem/U647591)- [T3](/problem/U647592)- [T4](/problem/U647593)
## T1 英语听写 / Writing Qualification
### 思路
4 行未改动```cpp#include <bits/stdc++.h>using namespace std;string s; int N, cnt;int mian() int main(){ cin >> N >> s; cnt = 0; int st[5] = {0}; for(char ch : s)56 行未改动 return 0;}```
[提交记录](/record/256520500)。[提交记录](/record/256903548)。
## T3 随机做题 / Random Examination
### 思路
注意到时间限制。注意到模数。$O(nm)$ 不会 T。概率 DP。注意到时间限制。注意到模数。
#### 定义定义 $dp_{i, j}$ 为做到第 $i$ 道题后总分对 $m$ 的余数为 $j$ 的概率。
$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$。
11 行未改动 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的概率为1 dp[0][0] = 1; //还没做题时总分对m的余数为0的概率为1 for(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;5 行未改动}```
[提交记录](/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]; //tree else 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)。