比赛 |
4043级2023省选模拟赛5 |
评测结果 |
AAAAAAAAAAAAAAAAA |
题目名称 |
Leaders |
最终得分 |
100 |
用户昵称 |
zxhhh |
运行时间 |
0.048 s |
代码语言 |
C++ |
内存使用 |
3.19 MiB |
提交时间 |
2023-03-27 19:43:24 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
typedef long long ll;
int n, dp[N][2], s[N][2], r[N];
char str[N];
ll ans;
int main () {
freopen("leaders.in", "r", stdin);
freopen("leaders.out", "w", stdout);
scanf("%d%s", &n, str+1);
for (int i = 1;i <= n;i++) scanf("%d", &r[i]);
for (int i = 1;i <= n;i++) {
s[i][0] = s[i-1][0];
s[i][1] = s[i-1][1];
if (str[i] == 'G') s[i][0]++;
else s[i][1]++;
}
for (int i = n;i >= 1;i--) {
int op = str[i]=='G'?0:1;
dp[i][0] = dp[i+1][0];
dp[i][1] = dp[i+1][1];
if (s[r[i]][op]-s[i-1][op]==s[n][op]) {
ans += dp[i+1][op^1]; dp[i][op]++;
}
else ans += dp[i+1][op^1]-dp[r[i]+1][op^1];
// cout << ans << " " << dp[i][0] << " " << dp[i][1] << endl;
}
printf("%lld\n", ans);
return 0;
}