题目链接:

  http://codeforces.com/problemset/problem/671/B

题目大意:

  N个人,每个人有Ci钱,现在有一个人劫富济贫,从最富的人之一拿走1元,再给最穷的人。总共K次,问最后贫富差距。

  钱被拿走是立刻结算,所以可能拿走后这个人变最穷的人再还回去。

  最富或最穷的人可能有多个,随机选择,并且不会影响最终答案。

  (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109)

题目思路:

  【模拟】

  直接排序离散化数据,之后模拟就行。

  细节挺多的要处理清楚。

  1. //
  2. //by coolxxx
  3. //#include<bits/stdc++.h>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<string>
  7. #include<iomanip>
  8. #include<map>
  9. #include<memory.h>
  10. #include<time.h>
  11. #include<stdio.h>
  12. #include<stdlib.h>
  13. #include<string.h>
  14. //#include<stdbool.h>
  15. #include<math.h>
  16. #define min(a,b) ((a)<(b)?(a):(b))
  17. #define max(a,b) ((a)>(b)?(a):(b))
  18. #define abs(a) ((a)>0?(a):(-(a)))
  19. #define lowbit(a) (a&(-a))
  20. #define sqr(a) ((a)*(a))
  21. #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
  22. #define mem(a,b) memset(a,b,sizeof(a))
  23. #define eps (1e-8)
  24. #define J 10
  25. #define mod 1000000007
  26. #define MAX 0x7f7f7f7f
  27. #define PI 3.14159265358979323
  28. #define N 500004
  29. using namespace std;
  30. typedef long long LL;
  31. int cas,cass;
  32. int n,m,lll,ans;
  33. double anss;
  34. LL b[N];
  35. LL sum;
  36. struct xxx
  37. {
  38. LL num,large;
  39. }a[N];
  40. bool cmp(LL aa,LL bb)
  41. {
  42. return aa<bb;
  43. }
  44. int work()
  45. {
  46. int i;
  47. LL k,maxx,minn;
  48. k=m;i=lll;
  49. while(i && k>=a[i].num*(a[i].large-a[i-].large))
  50. {
  51. k-=a[i].num*(a[i].large-a[i-].large);
  52. a[i-].num+=a[i].num;
  53. a[i--].num=;
  54. if(!i)return (sum%n!=);
  55. }
  56. maxx=a[i].large-k/a[i].num;
  57. a[i+].large=maxx,a[i+].num=a[i].num-k%a[i].num;
  58. a[i].large=maxx-;a[i].num-=a[i+].num;
  59. lll=i+;
  60. //==========================================
  61. k=m;i=;
  62. while(k> && k>=a[i].num*(a[i+].large-a[i].large) && a[i].large<maxx)
  63. {
  64. if(a[i].num==){i++;continue;}
  65. k-=a[i].num*(a[i+].large-a[i].large);
  66. a[i+].num+=a[i].num;
  67. a[i++].num=;
  68. }
  69. if(a[i].large>=maxx)return (sum%n!=);
  70. if(a[i+].num==)
  71. minn=a[i].large+k/a[i].num;
  72. else
  73. minn=a[i].large+k/(a[i].num);
  74. return maxx-minn;
  75. }
  76. int main()
  77. {
  78. #ifndef ONLINE_JUDGE
  79. freopen("1.txt","r",stdin);
  80. // freopen("2.txt","w",stdout);
  81. #endif
  82. int i,j;
  83. // for(scanf("%d",&cas);cas;cas--)
  84. // for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
  85. // while(~scanf("%s",s))
  86. while(~scanf("%d",&n))
  87. {
  88. scanf("%d",&m);
  89. sum=;mem(a,);
  90. for(i=;i<=n;i++)
  91. {
  92. scanf("%d",&b[i]);
  93. sum+=b[i];
  94. }
  95. sort(b+,b++n,cmp);
  96. lll=;a[lll].large=b[];a[lll].num=;
  97. for(i=;i<=n;i++)
  98. {
  99. if(b[i]==b[i-])a[lll].num++;
  100. else a[++lll].large=b[i],a[lll].num=;
  101. }
  102. j=work();
  103. printf("%d\n",j);
  104. }
  105. return ;
  106. }
  107. /*
  108. //
  109.  
  110. //
  111. */

【模拟】Codeforces 671B Robin Hood的更多相关文章

  1. codeforces 671B Robin Hood 二分

    题意:有n个人,每个人a[i]个物品,进行k次操作,每次都从最富有的人手里拿走一个物品给最穷的人 问k次操作以后,物品最多的人和物品最少的人相差几个物品 分析:如果次数足够多的话,最后的肯定在平均值上 ...

  2. Codeforces 672D Robin Hood(二分好题)

    D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. CodeForces 672D Robin Hood

    思维. 当$k$趋向于正无穷时,答案会呈现出两种情况,不是$0$就是$1$.我们可以先判断掉答案为$1$和$0$的情况,剩下的情况都需要计算. 需要计算的就是,将最小的几个数总共加$k$次,最小值最大 ...

  4. Codeforces 671B/Round #352(div.2) D.Robin Hood 二分

    D. Robin Hood We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and ...

  5. Codeforces Round #352 (Div. 1) B. Robin Hood 二分

    B. Robin Hood 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...

  6. 【CodeForces】671 B. Robin Hood

    [题目]B. Robin Hood [题意]给定n个数字的序列和k次操作,每次将序列中最大的数-1,然后将序列中最小的数+1,求最终序列极差.n<=5*10^5,0<=k<=10^9 ...

  7. Codeforces Round #352 (Div. 2) D. Robin Hood 二分

    D. Robin Hood   We all know the impressive story of Robin Hood. Robin Hood uses his archery skills a ...

  8. Codeforces Round #352 (Div. 1) B. Robin Hood (二分)

    B. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. Codeforces Round #352 (Div. 1) B. Robin Hood

    B. Robin Hood 讲道理:这种题我是绝对不去(敢)碰的.比赛时被这个题坑了一把,对于我这种不A不罢休的人来说就算看题解也要得到一个Accepted. 这题网上有很多题解,我自己是很难做出来的 ...

随机推荐

  1. myEclipse修改deploy location

  2. Activity---Fragment---listView的实现

    我们要做的是在Activity中加入一个ViewPager,利用ViewPager的适配器(继承于FragmentPagerAdapter)将Fragment加到其中,而我们在又在Fragment中又 ...

  3. xcode7 icon图标设置

  4. 【转】伟大的RAC和MVVM入门(一)

    原文:http://www.sprynthesis.com/2014/12/06/reactivecocoa-mvvm-introduction/   翻译自ReactiveCocoa and MVV ...

  5. 在Mac OS上搭建本地服务器

    我们在做网络编程的时候一般是需要有网络环境的,这样可以边写边测试达到很高的效率.但有些时候我们由于很多原因我们的电脑无法连接到网络,这时就会感觉很不自在,所以今天在这里教大家怎么用自己电脑作服务器. ...

  6. 【POJ2266】【树状数组+离散化】Ultra-QuickSort

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  7. 我用Emacs,后来转向Vim——Vim学习之Vim键盘图(绝对值得珍藏)

    Emacs本来就比较臃肿,麻烦.当我发现Vim键盘图时,我就渐渐转向Vim,追随Unix/Linux哲学去了.. 我用了Emacs三个月,因为它的学习曲线没Vim陡,这点吸引了,我使用Linux才7. ...

  8. HTML5的Server-Sent Events (SSE)

    HTML5有一个Server-Sent Events(SSE)功能,允许服务端推送数据到客户端.(通常叫数据推送).我们来看下,传统的WEB应用程序通信时的简单时序图: 现在Web App中,大都有A ...

  9. JS判断是否为一个数组

    function isArray(object){ return object && typeof object==='object' && Array == obje ...

  10. POJ1258-Agri-Net-ACM

    Description Farmer John has been elected mayor of his town! One of his campaign promises was to brin ...