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. C#基础学习之StreamReader和StreamWriter

    StreamReader和StreamWriter操作字符的 FileStream操作字节的 //使用StreamReader读取文件 using (StreamReader sr=new Strea ...

  2. 02 Go 1.2 Release Notes

    Go 1.2 Release Notes Introduction to Go 1.2 Changes to the language Use of nil Three-index slices Ch ...

  3. python基础--shutil模块

    shutil模块提供了大量的文件的高级操作. 特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 注意 即便是更高级别的文件复制函数(shutil.cop ...

  4. jupyter(ipython notebook) 安装和入门教程

    近期大家无论是自己做数据分析还是紧急答辩做PPT,可能都需要画一些数据的展示图:以前大家都是用excel画图,但excel画图存在一定的局限性,比如你要画个累积直方图,excel就很麻烦了,所以给大家 ...

  5. C++之可调用对象

    C++中的可调用对象分为以下几种: 函数 函数指针 lambda表达式 bind创建的对象 重载了函数调用运算符(即“()”)的类 函数.函数指针不再介绍.lambda表达式与bind创建的类参考—— ...

  6. Mac OS 下安装mysqlclient报“mysql_config not found”的解决

    如问题所示,应该是你没有将mysql_config所在文件夹加入系统的PATH路径,解决方案下: 1.第一步找到你的mysql_config所在位置 1.1. 如果是直接安装mysql,所在位置应该是 ...

  7. tidb 记录文档

    ansible-playbook stop.yml   / start.yml 重启集群,在ansible目录下执行 SHOW STATS_META; 查看统计信息 重启集群:ansible-play ...

  8. Loadrunner11之禁用/启用Action

    Loadrunner11之禁用/启用Action   大家在用Loadrunner11录制脚本的时候,会产生多个action,比如login, search, logout.在调试脚本的时候,我不想从 ...

  9. Buffer对象的总结

    1.首先记录下php关于字符串长度的统计 <?php echo '<br>'; echo '<br>'; 2.Buffer对象为16进制的两位数,即FF 0-225: v ...

  10. 关于日志API接口中流量的使用。

    现状: 目前服务器使用带宽是2M,那么最大上行流量应该是250kb/s,而通过日志发现目前最大并发流量是350kb/s. 问题: 看到以上的结果时,我当时的疑问是最大并发流量超过了服务器最大上行流量, ...