首先求出B和W的个数,如果只出现了一种那么直接输出sum(k)。

否则依次扫描,能割就割,时间复杂度$O(n)$。

#include<cstdio>
#define N 100010
typedef long long ll;
int T,n,i,a[N],b[N],c[2],d[2],ans;ll x;char s[2];
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
c[0]=c[1]=d[0]=d[1]=ans=0;
for(i=1;i<=n;i++)scanf("%d%s",&b[i],s),a[i]=s[0]=='B',c[a[i]=s[0]=='B']+=b[i];
if(!c[0]||!c[1]){printf("%d\n",c[0]+c[1]);continue;}
for(i=1;i<=n;i++){
x=(ll)c[a[i]]*d[a[i]^1]-(ll)d[a[i]]*c[a[i]^1];
if(x%c[a[i]^1]||x<=0||x/c[a[i]^1]>b[i])d[a[i]]+=b[i];
else ans++,d[a[i]]=b[i]-x/c[a[i]^1],d[a[i]^1]=0;
}
printf("%d\n",ans);
}
return 0;
}

  

BZOJ4045 : [Cerc2014] bricks的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. Box of Bricks最小移动砖块数目

    Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and ...

  4. Codeforces Gym 100002 B Bricks 枚举角度

    Problem B Bricks" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100002 ...

  5. [POJ1477]Box of Bricks

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19503   Accepted: 7871 Description Litt ...

  6. HDOJ(HDU) 2088 Box of Bricks(平均值)

    Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon ano ...

  7. HDOJ 1326 Box of Bricks(简单题)

    Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon ano ...

  8. 591 - Box of Bricks

     Box of Bricks  Little Bob likes playing with his box of bricks. He puts the bricks one upon another ...

  9. Box of Bricks

    Box of Bricks Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

随机推荐

  1. 运行fdisk命令时,弹出 bash:fdisk:command not found

    原因:命令fdisk 不在你的命令搜索路径中 第一种解决办法,将fdisk添加到你的命令搜索路径中 首先查看你当前的命令搜索路径: [root@host sbin]# echo $PATH/usr/k ...

  2. ActiveMQ基础教程JMS概述

    什么是JMS JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息 ...

  3. bzoj千题计划226:bzoj2763: [JLOI2011]飞行路线

    http://www.lydsy.com/JudgeOnline/problem.php?id=2763 这也算分层图最短路? dp[i][j]到城市i,还剩k次免费次数的最短路 #include&l ...

  4. pandas重置索引的几种方法探究

    pandas重置索引的几种方法探究 reset_index() reindex() set_index() 函数名字看起来非常有趣吧! 不仅如此. 需要探究. http://nbviewer.jupy ...

  5. android中实现在ImageView上随意画线涂鸦

    我实现的思路: 1.继承ImageView类 2.重写onTouchEvent方法,在ACTION_MOVE(即移动时),记录下所经过的点坐标,在ACTION_UP时(即手指离开时,这时一条线已经画完 ...

  6. 移动端网页开发 meta 之 viewport

    这几天在搞微信公众平台,涉及到几个页面要写,可是当我测试时发现和想象中不太一样,于是去找了几个页面看了下,发现他们页面头部有 meta viewport 的标签,所以去找了下网上的资料,发现千篇一律, ...

  7. 【51Nod】1055 最长等差数列 动态规划

    [题目]1055 最长等差数列 [题意]给定大小为n的互不不同正整数集合,求最长等差数列的长度.\(n \leq 10000\). [算法]动态规划 两个数之间的差是非常重要的信息,设\(f_{i,j ...

  8. Anaconda+django写出第一个web app(八)

    今天来实现网站的登入和登出功能. 首先我们需要在urls.py中添加路径,注意此处的路径和在导航栏中设置的文字路径保持一致: from django.urls import path from . i ...

  9. 001_fpm打包命令详解

    使用fpm来制作rpm包 2017/2/22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  10. shell脚本 ------ 输出带颜色的字体

    shell脚本中echo显示内容带颜色显示,echo显示带颜色,需要使用参数-e 格式如下: echo -e “\033[字背景颜色:文字颜色m字符串\033[0m” 例如: echo -e “\03 ...