CodeForces 672D 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.

Example
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次,每次操作是将最大的数减1,并且将最小的数加1,操作k次之后,输出此时最大的数与最小的数的差值。

这道题可以分成两步解题:

第一步:如果每次将最小值加1,操作k次,求k次之后的最小值。对于二分,我们需要首先确定上下界,这题的下界是初始最小的那个数,根据题目中的意思,我们将c数组升序排过序之后是c[0]。对于上界:上界是独特的,不能单独考虑这一步,因为把最大值减1,将最小值加1,这种操作,有一个临界点,就是当没有最大值,或者没有最小值的时候,数组中的数就不会发生改变。所以问题来了,上界是什么,我们将sum代表c数组所有元素的求和,那么当无限次操作之后,c数组中的元素就会等于sum/n(即平均值,当然还需要考虑sum%n是否等于0的情况,这里就不多说了,在代码中必须写),所以这一步的上界就是sum/n,再操作也不会最小的那个说也不会超过这个值(因为已经是平均值了,这一步需要多多理解),然后现在确定了上下界,二分找最小值就可以了。

第二步:如果每次将最大值减1,操作k次,求k次之后的最大值。然后我们确立二分的上下界,上界毫无疑问就是升序排过序之后的c[n-1],这里要特别考虑一下下界,因为整体考虑最大值再怎么减也不会比平均值小,所以下界就是sum/n(当然sum%n不为0的时候下界是sum/n+1)。然后就确定了上下界,二分找最大值就可以了。

第一步和第二步完成之后,直接就可以输出结果了,具体请看代码:

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

#define ll long long
const int maxn = 5*1e5+5;
ll n, k;
ll c[maxn];
ll ansl, ansr;//ansl最小值,ansr最大值。
int judgemi(ll x) {
    ll sum = 0;
    for(int i = 0; i < n; i++) {
        if(c[i] > x) break;
        sum += x-c[i];
    }
    if(sum <= k) return 1;
    return 0;
}
int judgemx(ll x) {
    ll sum = 0;
    for(int i = n-1; i >= 0; i--) {
        if(x > c[i]) break;
        sum += c[i]-x;
    }
    if(sum <= k) return 1;
    return 0;
}
void searchmi(ll l, ll r) { //二分找最小值
    ll left = l, right = r, mid;
    left = l, right = r;
    while(left + 4 < right) {
        mid = (left + right)/2;
        if(judgemi(mid)) left = mid;
        else right = mid;
    }
    for(ll i = right; i >= left; i--) {
        if(judgemi(i)) {
            ansl = i;
            break;
        }
    }
}
void searchmx(ll l, ll r) { //二分找最大值
    ll left = l, right = r, mid;
    left = l, right = r;
    while(left + 4 < right) {
        mid = (left+right)/2;
        if(judgemx(mid)) right = mid;
        else left = mid;
    }
    for(ll i = left; i <= right; i++) {
        if(judgemx(i)) {
            ansr = i;
            break;
        }
    }
}
int main() {
    while(~scanf("%lld %lld", &n, &k)) {
            ll sum = 0;
        for(int i = 0; i < n; i++) {
            scanf("%lld", &c[i]);
            sum += c[i];
        }
        sort(c, c+n);
        ll l, r, left, right, mid;
        if(sum%n == 0) {
            l = sum/n;
            r = sum/n;
        }
        else {
            l = sum/n;
            r = 1 + sum/n;
        }
        searchmi(c[0], l);
        searchmx(r, c[n-1]);
        printf("%lld\n", ansr - ansl);//输出
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值