题意就是说有串BW的字符,让你按一定的黑白数量比例来划分,求最大的划分后的块数;
直接贪心o(n)的复杂度,如果下一子串是黑,我们就看按现在tmpwhite的数量来换算所需要的black再
减掉现在有的tmpblack来取(注意需要判断所需要的black必须大于现在的tmpblack),如果现在的黑的数量不够取
就只能全取,具体看代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long LL;
const int N=100000+100;
int a[N];
char s[N];
LL gcd(LL x,LL y)
{
return y==0?x:gcd(y,x%y);
}
int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
LL b=0,w=0;
for(int i=0;i<n;i++)
{
scanf("%d %c",a+i,&s[i]);
if(s[i]=='B')b+=a[i];
else w+=a[i];
}
if(!w||!b){
printf("%lld\n",w+b);//lld要注意啦
continue;
}
LL tmp=gcd(b,w);
b/=tmp,w/=tmp;//黑跟白的比例
int ok=-1;
LL tb=0,tw=0;//存临时(tmp)的black,跟white的数量
int cnt=0;
for(int i=0;i<n;i++)
{
if(s[i]=='B'){
if(tw*b%w==0&&tw*b>=w&&tw*b/w-tb<=a[i]&&tw*b/w>=tb){
tb=a[i]-tw*b/w+tb;
tw=0;
cnt++;
}
else tb+=a[i];
}
else {
if(tb*w%b==0&&tb*w>=b&&tb*w/b-tw<=a[i]&&tb*w/b>=tw){
tw=a[i]-tb*w/b+tw;
tb=0;
cnt++;
}
else tw+=a[i];
}
}
cout<<cnt<<endl;
}
return 0;
}