比赛 |
2024暑假C班集训C |
评测结果 |
AAAAAAAAAA |
题目名称 |
W&B |
最终得分 |
100 |
用户昵称 |
darkMoon |
运行时间 |
1.710 s |
代码语言 |
C++ |
内存使用 |
3.76 MiB |
提交时间 |
2024-07-12 10:22:37 |
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
// #define fin cin
// #define fout cout
ifstream fin("silly.in");
ofstream fout("silly.out");
auto mread = [](){
int x;
fin >> x;
return x;
};
int t = mread(), ans;
signed main(){
while(t --){
int n = mread();
char c[n + 5];
int a[n + 5], sw = 0, sb = 0;
for(int i = 1; i <= n; i ++){
a[i] = mread();
fin >> c[i];
if(c[i] == 'B'){
sb += a[i];
}
else if(c[i] == 'W'){
sw += a[i];
}
}
if(sw == 0 || sb == 0){
fout << (sw | sb) << "\n";
continue;
}
{
int tmp = __gcd(sw, sb);
sw /= tmp, sb /= tmp;
}
int w = 0, b = 0, ans = 0;
for(int i = 1; i <= n; i ++){
if(c[i] == 'B'){
if(w % sw == 0 && b * sw < sb * w && (b + a[i]) * sw >= w * sb){
int tob = w / sw * sb;
a[i] -= tob - b;
b = w = 0, ans ++;
if(a[i] > 0){
i --;
}
}
else{
b += a[i];
}
}
else{
if(b % sb == 0 && b * sw > sb * w && b * sw <= (w + a[i]) * sb){
int tow = b / sb * sw;
a[i] -= tow - w;
b = w = 0, ans ++;
if(a[i] > 0){
i --;
}
}
else{
w += a[i];
}
}
}
fout << ans << "\n";
}
return 0;
}