模拟赛四道题题解

· · 题解

目录

T1 英语听写 / Writing Qualification

思路

状态机。

代码

#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;
}

提交记录。

T2 连续机惨 / Rapid Operation

思路

区间 DP。似乎和最大子段和只差一个 k。

设 dp_{i, j, 0/1} 表示前 i 台电脑,选择了 j 段,是否机惨第 i 台电脑的最大和。

状态转移

不选 a_i:

选 a_i:

代码

#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;
}

提交记录。

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。

代码

#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的概率为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;
        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;
}

提交记录。

T4 迷宫连通 / Tree Renovation

题意

wsb 的迷宫里有三种格子:

思路

将每个格子视为图中节点,从当前格子向四个方向扩展:

注意到所有边权均 \geqslant 0,考虑带权 Dijkstra。

复杂度分析

时间复杂度:O(nm \log(nm))。

空间复杂度:O(nm)。

代码

#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;
}

提交记录。