大意: 给定数组$a$, 每次操作使最大元素减小1最小元素增大1, 求k次操作后最大值与最小值的差.

二分出k次操作后最大值的最小值以及最小值的最大值, 若和能平分答案即为$max(0,R-L)$, 否则为$max(1,R-L)$

  1. #include <iostream>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstdio>
  5. #include <math.h>
  6. #include <set>
  7. #include <map>
  8. #include <queue>
  9. #include <string>
  10. #include <string.h>
  11. #include <bitset>
  12. #define REP(i,a,n) for(int i=a;i<=n;++i)
  13. #define PER(i,a,n) for(int i=n;i>=a;--i)
  14. #define hr putchar(10)
  15. #define pb push_back
  16. #define lc (o<<1)
  17. #define rc (lc|1)
  18. #define mid ((l+r)>>1)
  19. #define ls lc,l,mid
  20. #define rs rc,mid+1,r
  21. #define x first
  22. #define y second
  23. #define io std::ios::sync_with_stdio(false)
  24. #define endl '\n'
  25. #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
  26. using namespace std;
  27. typedef long long ll;
  28. typedef pair<int,int> pii;
  29. const int P = 1e9+7, INF = 0x3f3f3f3f;
  30. ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
  31. ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
  32. ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
  33. inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
  34. //head
  35.  
  36. #ifdef ONLINE_JUDGE
  37. const int N = 1e6+10;
  38. #else
  39. const int N = 111;
  40. #endif
  41.  
  42. int n, k, a[N];
  43. int main() {
  44. scanf("%d%d", &n, &k);
  45. REP(i,1,n) a[i]=rd();
  46. int l=0,r=1e9,L,R;
  47. while (l<=r) {
  48. ll d = 0;
  49. REP(i,1,n) if (a[i]<mid) d+=mid-a[i];
  50. if (d<=k) L=mid,l=mid+1;
  51. else r=mid-1;
  52. }
  53. l=0,r=1e9;
  54. while (l<=r) {
  55. ll d = 0;
  56. REP(i,1,n) if (a[i]>mid) d+=a[i]-mid;
  57. if (d<=k) R=mid,r=mid-1;
  58. else l=mid+1;
  59. }
  60. int ans = max(0,R-L);
  61. ll sum = 0;
  62. REP(i,1,n) sum+=a[i];
  63. if (sum%n) ans=max(ans,1);
  64. printf("%d\n", ans);
  65. }

Robin Hood CodeForces - 672D (二分)的更多相关文章

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

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

  2. codeforces 672D D. Robin Hood(二分)

    题目链接: D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. 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 ...

  4. 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 ...

  5. CF 672D Robin Hood(二分答案)

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

  6. 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 ...

  7. 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 ...

  8. 【15.93%】【codeforces 672D】Robin Hood

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【CodeForces】671 B. Robin Hood

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

随机推荐

  1. ubuntu16.04下g++安装及使用

    1)首先在虚拟机中安装Ubuntu16.04,网络模式设置为NAT模式,安装完成后在虚拟机中测试是否能够上网. 2)进入Ubuntu,按Ctrl+alt+T,调出终端,输入sudo su,输入密码切换 ...

  2. luogu P1003 铺地毯

    水题 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; t ...

  3. Spring boot 源码分析(前言)

    开坑达人 & 断更达人的我又回来了 翻译的坑还没填完,这次再开个新坑= = 嗯,spring boot的源码分析 本坑不打算教你怎么用spring boot = = 也不打算跟你讲这玩意多方便 ...

  4. php配置可被设定范围

    PHP中的每个指令都有其所属的模式,这些模式决定这一个PHP指定在何时何地.是否能被设定.例如有些指令可以在 PHP 脚本中用 ini_set() 来设定,而有些则只能在php.ini 或 httpd ...

  5. rabbitMQ Management http://localhost:15672/ 打不开

    C:\RabbitMQ Server\rabbitmq_server-3.7.7\sbin>rabbitmq-plugins enable rabbitmq_management 安装rabbi ...

  6. ssm框架搭建的基本配置(一站式教会你搭建)

    首先是需要的jar包: <dependency> <groupId>org.springframework</groupId> <artifactId> ...

  7. shell编辑器vi的常用命令

    一:翻页 ctrl+u向上翻半页 ctrl+f向上翻一页 ctrl+d 向下翻半页 ctrl+b 向下翻一页 二:移动光标指令 0: 光标移至当前行首 $: 光标移至当前行尾 三:常用插入.删除指令 ...

  8. 豆瓣上关于<<一万小时天才理论>>一书的一个评论

    原帖地址:http://book.douban.com/review/3707543/ 这本书和其他几本关于一万小时的书一样,都是基于埃里克森和赫伯特.西蒙的“十年法则”的标志性理论,但这本书通过新的 ...

  9. js中创建对象的4种方法

    1.直接创建,不可复用式创建var obj = new Object(); obj.name = ""; obj.id = ""; 2.使用工厂方法来创建对象, ...

  10. python调用远程chromedriver.exe、selenium抓包方法

    本地python示例代码: from selenium import webdriver driver = webdriver.Remote(command_executor='http://192. ...