UVALive 6935 Bricks题解

题目链接

题意就是说有串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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值