【模拟】Codeforces 671B Robin Hood
2016-08-18 18:26
483 查看
题目链接:
http://codeforces.com/problemset/problem/671/B
题目大意:
N个人,每个人有Ci钱,现在有一个人劫富济贫,从最富的人之一拿走1元,再给最穷的人。总共K次,问最后贫富差距。
钱被拿走是立刻结算,所以可能拿走后这个人变最穷的人再还回去。
最富或最穷的人可能有多个,随机选择,并且不会影响最终答案。
(1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109)
题目思路:
【模拟】
直接排序离散化数据,之后模拟就行。
细节挺多的要处理清楚。
View Code
http://codeforces.com/problemset/problem/671/B
题目大意:
N个人,每个人有Ci钱,现在有一个人劫富济贫,从最富的人之一拿走1元,再给最穷的人。总共K次,问最后贫富差距。
钱被拿走是立刻结算,所以可能拿走后这个人变最穷的人再还回去。
最富或最穷的人可能有多个,随机选择,并且不会影响最终答案。
(1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109)
题目思路:
【模拟】
直接排序离散化数据,之后模拟就行。
细节挺多的要处理清楚。
// //by coolxxx //#include<bits/stdc++.h> #include<iostream> #include<algorithm> #include<string> #include<iomanip> #include<map> #include<memory.h> #include<time.h> #include<stdio.h> #include<stdlib.h> #include<string.h> //#include<stdbool.h> #include<math.h> #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define abs(a) ((a)>0?(a):(-(a))) #define lowbit(a) (a&(-a)) #define sqr(a) ((a)*(a)) #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) #define mem(a,b) memset(a,b,sizeof(a)) #define eps (1e-8) #define J 10 #define mod 1000000007 #define MAX 0x7f7f7f7f #define PI 3.14159265358979323 #define N 500004 using namespace std; typedef long long LL; int cas,cass; int n,m,lll,ans; double anss; LL b ; LL sum; struct xxx { LL num,large; }a ; bool cmp(LL aa,LL bb) { return aa<bb; } int work() { int i; LL k,maxx,minn; k=m;i=lll; while(i && k>=a[i].num*(a[i].large-a[i-1].large)) { k-=a[i].num*(a[i].large-a[i-1].large); a[i-1].num+=a[i].num; a[i--].num=0; if(!i)return (sum%n!=0); } maxx=a[i].large-k/a[i].num; a[i+1].large=maxx,a[i+1].num=a[i].num-k%a[i].num; a[i].large=maxx-1;a[i].num-=a[i+1].num; lll=i+1; //========================================== k=m;i=1; while(k>0 && k>=a[i].num*(a[i+1].large-a[i].large) && a[i].large<maxx) { if(a[i].num==0){i++;continue;} k-=a[i].num*(a[i+1].large-a[i].large); a[i+1].num+=a[i].num; a[i++].num=0; } if(a[i].large>=maxx)return (sum%n!=0); if(a[i+1].num==0) minn=a[i].large+k/a[i].num; else minn=a[i].large+k/(a[i].num); return maxx-minn; } int main() { #ifndef ONLINE_JUDGE freopen("1.txt","r",stdin); // freopen("2.txt","w",stdout); #endif int i,j; // for(scanf("%d",&cas);cas;cas--) // for(scanf("%d",&cas),cass=1;cass<=cas;cass++) // while(~scanf("%s",s)) while(~scanf("%d",&n)) { scanf("%d",&m); sum=0;mem(a,0); for(i=1;i<=n;i++) { scanf("%d",&b[i]); sum+=b[i]; } sort(b+1,b+1+n,cmp); lll=1;a[lll].large=b[1];a[lll].num=1; for(i=2;i<=n;i++) { if(b[i]==b[i-1])a[lll].num++; else a[++lll].large=b[i],a[lll].num=1; } j=work(); printf("%d\n",j); } return 0; } /* // // */
View Code
相关文章推荐
- java之旅20160818
- Android studio常用的几个插件
- React.js学习版1~
- 打通SSH KEYS(ssh无密码)
- Linux资源监控命令/工具(综合)
- SqlServer中with(nolock)解读
- 位段的数据结构- 数据排列和打印
- Android基础知识整理
- PHP上传文件
- 史上最全的WebSettings说明
- group by 多行的最大最小值 http://www.bubuko.com/infodetail-706756.html
- 怎样防止App在后台运行,点击应用桌面图标重新启动?--亲测有效
- 用python编写第一个IDA插件
- cocos2d-x 资源优化
- ComboBox.Text="System.Data.DataRowView"的问题
- Openfire使用上的一些技巧
- 残差金子塔邻域信息融合
- jQuery源码分析-01总体架构
- php 代码规范
- HIHO #1176 : 欧拉路·一