Bricks Gym - 100543I

#include<map>
#include<queue>
#include<time.h>
#include<limits.h>
#include<cmath>
#include<ostream>
#include<iterator>
#include<set>
#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep_1(i,m,n) for(int i=m;i<=n;i++)
#define mem(st) memset(st,0,sizeof st)
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef pair<double,double> pdd;
const int N = 1e5 + 10;
#define x first
#define y second
ll b,w,gcd1;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ans;
ll numb,numw;
struct node
{
	ll num;
	char c;
} e[N];
int n;
void solve()
{
	scanf("%d",&n);
	ll b=0;
	ll w=0;
	for(int i=1; i<=n; i++)
	{
		scanf("%lld %c",&e[i].num,&e[i].c);
		if (e[i].c=='B')
			b+=e[i].num;
		else
			w+=e[i].num;
	}
	if(b==0)
		printf("%lld\n",w);
	else if(w==0)
		printf("%lld\n",b);
	else
	{
		ll gcd1=gcd(b,w);
		b/=gcd1,w/=gcd1;//最终的比
		ll ans=0;
		ll numb=0;
		ll numc=0;
		for(int i=1; i<=n; i++)
		{
			//如果是w  判断当前的B是否位最简比的倍数,如果是,判断能不能凑出w
			if (e[i].c=='W')
			{
				//当前是最大公约数的几倍
				ll beishu=numb/b;
				//     当前的数是最大公约数的倍数
				//					可以凑出当前倍数下的w
				if (numb && numb%b==0 && w*beishu>=numw && w*beishu<=numw+e[i].num)
				{
					ans++;
					numb=0;
					numw=numw+e[i].num-w*beishu;
				}
				//凑不出来,就直接加
				else
					numw+=e[i].num;
			}
			//同理
			else
			{
				ll beishu=numw/w;
				if (numw && numw%w==0 && b*beishu>=numb && b*beishu<=numb+e[i].num)
				{
					ans++;
					numw=0;
					numb=numb+e[i].num-b*beishu;
				}
				else
					numb+=e[i].num;
			}
		}
		printf("%lld\n",ans);
	}
}
signed main()
{
	int t;
	scanf("%d", &t);
	while(t--)
		solve();
	return 0;
}
posted @ 2020-05-11 09:43  晴屿  阅读(91)  评论(0编辑  收藏  举报