标题
增加 1 行删除 1 行
1四道题的题解1模拟赛四道题题解
正文
增加 93 行删除 11 行
12344 行未改动9101112131415161756 行未改动747576777879808182838485868788899091929394959697989910010110210311 行未改动1151161171181191201211221235 行未改动129130131132## T1 英语听写 / Writing Qualification### 思路4 行未改动```cpp#include <bits/stdc++.h>using namespace std;string s; int N, cnt;int mian(){cin >> N >> s; cnt = 0;int st[5] = {0};for(char ch : s)56 行未改动return 0;}```[提交记录](/record/256520500)。## T3 随机做题 / Random Examination### 思路注意到时间限制。注意到模数。$O(nm)$ 不会 T。#### 定义$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的概率为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;5 行未改动}```[提交记录](/record/256900804)。12345678910114 行未改动16171819202122232456 行未改动8182838485868788899091929394959697989910010110210310411 行未改动1161171181191201211221231245 行未改动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 main(){cin >> N >> s; cnt = 0;int st[5] = {0};for(char ch : s)56 行未改动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$。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的概率为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;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]; //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)。