D. Robin Hood

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.

Examples
input
4 1
1 1 4 2
output
2
input
3 1
2 2 2
output
0
Note

Lets look at how wealth changes through day in the first sample.

  1. [1, 1, 4, 2]
  2. [2, 1, 3, 2] or [1, 2, 3, 2]

So the answer is 3 - 1 = 2

In second sample wealth will remain the same for each person.

题意:给你n个数,每次能从最大的数一个移给最小的数,问k次操作后,最大和最小的数的差。

思路 : 题目可以简化成找最终状态下的最大值和最小值,那么分别进行二分最大值和最小值,此外注意二分最大、最小时的不同之处。

 #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std; const int MAX = ;
const int INF = 0x3f3f3f3f;
int a[MAX], n; int findmax(int x, int k)
{
for(int i = ; i < n; i++)
{
if(a[i] > x)
k-=a[i] - x;
if(k < )
return ;
}
return ;
}
int findmin(int x, int k)
{
for(int i = ; i < n; i++)
{
if(a[i] < x)
k-=x - a[i];
if(k < )
return ;
}
return ;
} int main()
{
int k;
__int64 sum = ;
int t1, t2;
cin >> n >> k;
t1 = ;
t2 = INF;
for(int i = ; i < n; i++)
{
scanf("%d", a + i);
sum += a[i];
t1 = max(t1, a[i]);
t2 = min(t2, a[i]);
}
int mx = sum / n + (sum%n> ? : );
int mi = sum / n; int l , r, maxx , minn;
/////mi
l = t2, r = mi;
while(l <= r) //注意等于
{
int mid = (l + r) >> ;
if(findmin(mid, k))
{
l = mid + ;
minn = mid;
}
else r = mid - ;
}
////ma
l = mx, r = t1;
while(l < r)
{
int mid = (l + r) >> ;
if(findmax(mid, k))
{
r = mid;
maxx = max(mid, maxx);
}
else l = mid + ;
}
maxx = (l + r) >>; //结束状态时取
printf("%d\n", maxx - minn);
}

Codeforces 671B/Round #352(div.2) D.Robin Hood 二分的更多相关文章

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

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

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

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

  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 Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  8. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  9. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

随机推荐

  1. VUE中组件的使用

    关于vue组件引用 使用Nodejs的方法 被引用的组件要暴露 module.exports={}; 引用时 用 var abc= require("组件的路径") 然后 就可以用 ...

  2. 福大软工1816:Alpha(7/10)

    Alpha 冲刺 (7/10) 队名:Jarvis For Chat 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.完成 ...

  3. ACM 第十四天

    字符串: 1.KMP算法(模式串达到1e6) 模式串达到1e4直接暴力即可. 字符串哈希 字符串Hash的种类还是有很多种的,不过在信息学竞赛中只会用到一种名为“BKDR Hash”的字符串Hash算 ...

  4. YaoLingJump开发者日志(二)

      熟悉了一点LGame里的套路,可以正式开工了.   增加了一个信息栏,显示得分.硬币数.生命值和当前关卡(仿照了超级玛丽的布局).   准备瑶玲的各种动画(静止.奔跑.跳跃.趴下.休息和死亡等). ...

  5. python爬虫--打开爬取页面

    def requests_view(response): import webbrowser requests_url = response.url base_url = '<head>& ...

  6. Jenkins系列-Jenkins用户权限和角色配置

    由于jenkins默认的权限管理体系不支持用户组或角色的配置,因此需要安装第三发插件来支持角色的配置,这边将使用Role Strategy Plugin,介绍页面:https://wiki.jenki ...

  7. Flink中的数据传输与背压

    一图道尽心酸: 大的原理,上游的task产生数据后,会写在本地的缓存中,然后通知JM自己的数据已经好了,JM通知下游的Task去拉取数据,下游的Task然后去上游的Task拉取数据,形成链条. 但是在 ...

  8. 发生dev_queue_xmit的时候,全部都是从ip_finish_output中来的吗

    也就是说啊,内核中的收发包的路径,很可能是经理driver_recv --> tcp -->driver_send这样一个过程,是个很长的路径呢...... 从dev_queue_xmit ...

  9. CSS3 过渡效果触发时机的问题

    像360deg过渡效果,做专题的同学应该是经常做的. <!DOCTYPE html> <html lang="en"> <head> <m ...

  10. Docker的结构(6-13)

    一.Docker的结构. Docker命令不清楚的时候可以在命令的最后加上--help Docker和虚拟机的区别? 虚拟机的实现原理是:先模拟出一套硬件,然后在这基础上跑一个操作系统,然后在这个操作 ...