UVAlive6935
来源:互联网 发布:手机麻将辅助软件 编辑:程序博客网 时间:2024/05/18 01:12
挺坑的一个题
没相除思路来感觉就~
比例的确定的
也即是每次B的总和与W的总和的比值
然后从前往后跑一遍即可
代码必须要加紧练,这个题找bug补了一个小时~~~
#include<bits/stdc++.h>using namespace std;typedef long long LL;bool k[100100];int a[101000];char s[10];#define p(a) printf("%d\n",a);LL gcd(LL a,LL b){ return b==0?a:gcd(b,a%b);}int main(){ int t; scanf("%d",&t); while(t--){ memset(k,0,sizeof(k)); int n;int w=0,b=0; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d%s",&a[i],s); if(s[0]=='B') b+=a[i]; else { w+=a[i]; k[i]=1; } } //printf("%d %d\n",b,w); if(b==0||w==0) { LL sum=0; for(int i=0;i<n;i++) sum+=a[i]; printf("%lld\n",sum); continue; } LL c=gcd(b,w); LL B=b/c,W=w/c; //printf("%d %d \n",W,B); LL ans=0; int w1=0,b1=0; for(int i=0;i<n;i++){ if(!w1&&!b1){ if(k[i]) w1+=a[i]; else b1+=a[i]; //printf("w1 %d b1 %d\n",w1,b1); } else if(k[i]){ LL left_w=b1*W/B; //printf("%lld %d %d %d %lld %lld\n",left_w,w1,a[i],b1,B,W); //printf("%d%d%d\n",left_w-w1<=a[i],left_w-w1>0,left_w*B==b1*W); if( left_w-w1<=a[i] && left_w-w1>0 && left_w*B==b1*W ){ //printf("fsa\n"); a[i]-=left_w-w1;++ans; b1=0,w1=a[i]; // cout<< "#"<<w1<<b1<<endl; } else { w1+=a[i]; } } else { LL left_b=w1*B/W; //cout<< "#"<<w1<<b1<<endl; if( (left_b-b1<=a[i]&&left_b-b1>0)&&left_b*W==w1*B){ // printf("bbbb\n"); a[i]-=left_b-b1;++ans; w1=0,b1=a[i]; //printf("w1 %d b1 %d\n",w1,b1); } else { b1+=a[i]; } } } printf("%lld\n",ans); } return 0;}
0 0
- UVAlive6935
- android studio在模拟器上显示中文乱码的解决方案
- 关于一个查找数组中是否存在某一个元素的问题
- HDU 1010 Tempter of the Bone 深搜+奇偶剪枝
- PAT Advanced 1013
- 2015.9.2组队赛 1006题(dp:前缀和优化,空间压缩)
- UVAlive6935
- mybatis-SQL语句映射文件(1)resultMap
- 英方法语学习
- 动态输出html一些效果失效的处理
- Windows Server 2008 R2开关机取消登录时要按Ctrl+Alt+Delete组合键登录的方法
- CentOS6.5 x64 Hadoop学习过程
- 程序设计与我的5年
- Android 运行中效验文件完整合法性
- xtion usb interface is not supported