记录编号 590887 评测结果 AAAAAAAAAA
题目名称 W&B 最终得分 100
用户昵称 GravatarAeeE5x 是否通过 通过
代码语言 C++ 运行时间 1.964 s
提交时间 2024-07-12 14:56:06 内存使用 3.77 MiB
显示代码纯文本
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct nod{
	int num;
	char col;
};
int gcd(int x,int y){
	if(x%y==0) return y;
	return gcd(y,x%y);
}
int main(){
	freopen("silly.in","r",stdin);
	freopen("silly.out","w",stdout);
	
	int t;scanf("%d",&t);
	for(int P=1;P<=t;P++){
		int n,bsum=0,wsum=0;
		queue<nod> que;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			int k;char c;scanf("%d %c",&k,&c);
			que.push((nod){k,c});
			if(c=='B') bsum+=k;	
			else wsum+=k;
		}
		if(bsum==0||wsum==0){
			printf("%d\n",max(bsum,wsum));
		}else{
			int q=gcd(bsum,wsum),bn=0,wn=0,ans=0;
			bsum/=q,wsum/=q;
			while(!que.empty()){
				nod fr=que.front();que.pop();
				
				if(fr.col=='B') bn+=fr.num;
				else wn+=fr.num;
				
				if(fr.col=='W'){
					if(bn%bsum!=0) continue;
					int k=bn/bsum;
					if(wn-fr.num<k*wsum&&k*wsum<=wn) wn-=k*wsum,bn=0,ans++;
				}else{
					if(wn%wsum!=0) continue;
					int k=wn/wsum;
					if(bn-fr.num<k*bsum&&k*bsum<=bn) bn-=k*bsum,wn=0,ans++;
				}
			}
			printf("%d\n",ans);
		}
	}
	
	return 0;
}