T1 英语听写 / Writing Qualification
思路
状态机。
代码
#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)
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。似乎和最大子段和只差一个 。
设 表示前 台电脑,选择了 段,是否机惨第 台电脑的最大和。
状态转移
不选 :
- 前一个也没选:。
- 前一个选了:。
选 :
- 前一个也选了: 和 属于同一段。
- 前一个没选: 是新的一段开始。
代码
#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
思路
注意到时间限制。注意到模数。 不会 T。
定义
为做到第 道题后总分对 的余数为 的概率。
状态转移
如果第 道题做错了,则得分与上次相同,。
如果第 道题做对了,则获得 的分数,上一次得分为 ,。
故 。
提示
如果直接开 dp[100100][1010] 显然会 MLE,所以使用滚动数组。
最终状态转移方程为 。
代码
#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;
}提交记录。