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

题意:

  给你一个序列

  每一天过后,序列的最大值-1,最小值+1, 问你k天之后,最大值-最小值是多少

题解:

  在给定k天下

  我们二分最大值尽可能的 小,最小值最可能的大就可以了

#include<bits/stdc++.h>
using namespace std;
const int N = 3e6+, M = 1e7, mod = 1e9+,inf = 1e9+;
typedef long long ll; int a[N],n,k;
int check1(int mx) {
ll kk = ,buji = ;
for(int i=;i<=n;i++) {
if(a[i]>mx) kk+=(a[i]-mx);
else {
buji+=(mx - a[i]);
}
}
if(kk<=k&&buji>=kk) return ;
else return ;
}
int check2(int mi) {
ll kk = ,buji = ;
for(int i=;i<=n;i++) {
if(a[i]<mi) kk+=(mi - a[i]);
else buji += (a[i] - mi);
}
if(kk<=k&&buji>=kk) return ;
else
return ;
}
int main() {
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a+n+);
int mx = a[n],mi = a[];
int r = a[n], l = a[];
while(l<=r) {
int mid = (l+r)>>;
if(check1(mid)) r = mid-,mx = mid;
else l = mid+;
}
r = a[n], l = a[];
while(l<=r) {
int mid = (l+r)>>;
if(check2(mid)) l = mid+, mi = mid;
else r = mid-;
}
//cout<<mx<<" "<<mi<<endl;
cout<<mx - mi<<endl;
return ;
}

Codeforces 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 (二分答案)

    题目链接: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. hdu1114 Piggy-Bank (DP基础 完全背包)

    链接:Piggy-Bank 大意:已知一只猪存钱罐空的时候的重量.现在的重量,已知若干种钱的重量和价值,猪里面装着若干钱若干份,求猪中的钱的价值最小值. 题解: DP,完全背包. g[j]表示组成重量 ...

  2. Asp.net面试题

    Asp.net核心技术思想 1.概述反射和序列化 反射:程序集包含模块,而模块包含类型,类型又包含成员.反射则提供了封装程序集.模块和类型的对象.您可以使用反射动态地创建类型的实例,将类型绑定到现有对 ...

  3. Java中的异常

    一.什么是异常 异常就是在程序的运行过程中所发生的不正常的事件,如所需文件找不到,网络连接不通或中断,算术运算出错(如被0除),数组下标越界,装载了一个不存在的类,对null的操作,类型转换异常等等. ...

  4. 建立你的第一个 Git 仓库

    虽然 Git 确实是被许多重要软件选作版本控制工具,但是并不是仅能用于这些重要软件;它也能管理你购物清单(如果它们对你来说很重要的话,当然可以了!).你的配置文件.周报或日记.项目进展日志.甚至源代码 ...

  5. 用Prime31实现Google Play In-App-Blling

    Android开发者想在海外赚钱,接入Google Play是不二选择,然而一堆英文文档对于像我这样的英语四级都木有过的可谓是苦恼之极.近段时间因工作需要研究了Unity接入Google Play的整 ...

  6. rocksdb 编译安装 日志

    Compilation RocksDB's library should be able to compile without any dependency installed, although w ...

  7. Linux统计文件个数

    查看某个文件夹下的文件个数用ls列目录,用grep过虑,再用wc统计即可 用ls -l列出后, 每一行对应一个文件或目录, 如果第一个字母为’-'则为普通文件, 若为’d'则为子目录 + +grep过 ...

  8. PHP session的实现原理

    PHP SESSION原理 我们知道,session是在服务器端保持用户会话数据的一种方法,对应的cookie是在客户端保持用户数据.HTTP协议是一种无状态协议,服务器响应完之后就失去了与浏览器的联 ...

  9. 什么是iis服务器

    IS 是Internet Information Server的缩写,它是微软公司主推的服务器,最新的版本是Windows2003里面包含的IIS 6.0,IIS与WindowNT Server完全集 ...

  10. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...