BZOJ4045 : [Cerc2014] bricks

首先求出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;
}

  

时间: 2024-12-20 00:16:28

BZOJ4045 : [Cerc2014] bricks的相关文章

ZOJ 3495 Lego Bricks

计算几何,暴力. 题目中有一句话:$The$ $mass$ $of$ $each$ $brick$ $is$ $equally$ $distributed$ $and$ $it$ $will$ $be$ $stable$ $if$ $it$ $is$ $placed$ $on$ $bases$ $or$ $stable$ $bricks$ $and$ $the$ $moment$ $of$ $it$ $can$ $be$ $zero$ $when$ $it$ $is$ $placed$. 核心原

HDU4385Moving Bricks【状压DP】

Moving Bricks Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 724    Accepted Submission(s): 280 Problem Description Brickgao used to be a real tall, wealthy, handsome man and you might know him

Box of Bricks(南阳oj)

Box of Bricks 时间限制:1000 ms  |  内存限制:65535 KB 描述 Bob在垒砖块,但是他垒的有些乱,但是他很懒,所以想花费最少的体力来让他垒好的砖块一样高,所以你需要做的就是算出最少需要移动的砖块 输入 多组测试数据,以EOF结束,第一行是一个n(1<=n<=50)表示有n摞砖 第二行是n个正整数k(1<=k<=100)表示每摞砖的砖块数量 输出 对于每组输入数据有两行输出,第一行是一个Case #x,表示x组测试数据,第二行是一句话The minim

zju 1251 Bricks box 砖盒----------hdu 1326

#include <iostream> using namespace std; int main() { int a[51],n,k=1; while(cin>>n,n) { int i,ans=0,s=0; for(i=0;i<n;i++) { cin>>a[i]; s+=a[i]; } s/=n; for(i=0;i<n;i++) if(a[i]>s)ans+=a[i]-s; cout<<"Set #"<&l

zoj 1251 Box of Bricks

Box of Bricks Time Limit: 2 Seconds      Memory Limit: 65536 KB Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. ``Look, I've built a wall!'', he tells his older sister Alice.

HDOJ 1326. Box of Bricks 纯水题

Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5994    Accepted Submission(s): 2599 Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one up

POJ 1477. Box of Bricks 纯水题

Box of Bricks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19552   Accepted: 7889 Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. "Look, I've buil

Box of Bricks最小移动砖块数目

Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. “Look, I've built a wall!”, he tells his older sister Alice. “Nah, you should make all stacks the same height. The

HDU1326_Box of Bricks【水题】

Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4540    Accepted Submission(s): 2031 Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one up