B. Robin Hood

题目连接:

http://www.codeforces.com/contest/671/problem/B

Description

We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.

There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.

After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.

Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) — the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.

The second line contains n integers, the i-th of them is ci (1 ≤ ci ≤ 109) — initial wealth of the i-th person.

Output

Print a single line containing the difference between richest and poorest peoples wealth.

Sample Input

4 1

1 1 4 2

Sample Output

2

题意

每次你要让最大数减一,然后让最小数加一

然后操作k次

问你最大值减去最小值是多少

题解:

首先这道题模拟是可以的,但是太麻烦了……

所以还是直接二分就好了

二分一个最小值

然后再二分一个最大值

因为你会加k,使得小于那个最小值的数都加成为大于等于他的数

你会减去k,使得大于那个数,都降为小于等于的那个数

所以直接二分就完啦

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+7;
int n,k,a[maxn];
long long sum=0;
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),sum+=a[i];
sort(a+1,a+1+n);
int l1=sum/n,r1=(sum+n-1)/n;
int l=0,r=l1,ansl=0;
while(l<=r)
{
int mid=(l+r)/2;
long long need=0;
for(int i=1;i<=n;i++)if(a[i]<=mid)need+=mid-a[i];
if(need<=k)ansl=mid,l=mid+1;
else r=mid-1;
}
l=r1,r=1e9;
int ansr=0;
while(l<=r)
{
int mid=(l+r)/2;
long long need=0;
for(int i=1;i<=n;i++)if(a[i]>mid)need+=a[i]-mid;
if(need<=k)ansr=mid,r=mid-1;
else l=mid+1;
}
cout<<ansr-ansl<<endl;
}

Codeforces Round #352 (Div. 1) B. Robin Hood 二分的更多相关文章

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

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

    题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...

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

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

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

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

  6. Codeforces Round #352 (Div. 2) D. Robin Hood

    题目链接: http://codeforces.com/contest/672/problem/D 题意: 给你一个数组,每次操作,最大数减一,最小数加一,如果最大数减一之后比最小数加一之后要小,则取 ...

  7. Codeforces Round #352 (Div. 2) ABCD

    Problems     # Name     A Summer Camp standard input/output 1 s, 256 MB    x3197 B Different is Good ...

  8. Codeforces Round #352 (Div. 2)

    模拟 A - Summer Camp #include <bits/stdc++.h> int a[1100]; int b[100]; int len; void init() { in ...

  9. Codeforces Round #352 (Div. 2) (A-D)

    672A Summer Camp 题意: 1-n数字连成一个字符串, 给定n , 输出字符串的第n个字符.n 很小, 可以直接暴力. Code: #include <bits/stdc++.h& ...

随机推荐

  1. android开发中常用的快捷键

    Eclipse快捷键-方便查找,呵呵,记性不好 行注释/销注释 Ctrl+/  块注释/销注释/XML注释 Ctrl+Shift+/   Ctrl+Shift+\查找 查找替换 Ctrl+H  Ctr ...

  2. MVVM模式用依赖注入的方式配置ViewModel并注册消息

    最初的想法 这次主要讨论下给View指定ViewModel的事情.一般来说给View指定ViewModel常用的方式有两种,一种是在View的后台代码中写DataContext = new ViewM ...

  3. liunx系统top命令详解

    ps: 1.按1可以进行 CPU各个和总CPU汇总的切换2.cpu0是最关键的,总控管理各个CPU 3.默认情况下仅显示比较重要的 PID.USER.PR.NI.VIRT.RES.SHR.S.%CPU ...

  4. 使用PyMongo访问需要认证的MongoDB

    Windows 10家庭中文版,Python 3.6.4,PyMongo 3.7.0,MongoDB 3.6.3,Scrapy 1.5.0, 前言 在Python中,使用PyMongo访问Mongod ...

  5. js函数前加分号和感叹号的作用

    js函数前加分号和感叹号是什么意思?有什么用? 一般看JQuery插件里的写法是这样的 (function($) { //... })(jQuery); 今天看到bootstrap的javascrip ...

  6. android studio 解决avd启动问题 ----waiting for target device come online

    android studio 模拟器打不开,一直停留在第三方.waiting for target  device  come online 问题解决方法 方法1.Android Emulator 未 ...

  7. 一张图来帮你理解 SOA

    SOA 曾经一度是技术领域中最难以理解的一个概念.SOA 似乎让很多人感到困惑 - 一般来讲这是由于人们认为它拥有几乎神奇的力量.事实上 SOA 只是一个很简单的概念:SOA 由诸如 C++ 和 Ja ...

  8. 20165203《Java程序设计》第三周学习总结

    教材学习内容总结 1.类: (1)类的声明:class+类名 (2)类体:成员变量的声明+方法(局部变量+语句) 注意: 方法体内声明的局部变量只在方法内有效和书写位置有关. 局部变量和成员变量同名: ...

  9. Nginx 虚拟主机 VirtualHost 配置

    Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高. 我在& ...

  10. hiho 1227 找到一个恰好包含n个点的圆 (2015北京网赛 A题)

    平面上有m个点,要从这m个点当中找出n个点,使得包含这n个点的圆的半径(圆心为n个点当中的某一点且半径为整数)最小,同时保证圆周上没有点. n > m 时要输出-1 样例输入43 2 0 0 1 ...