cf 671B Robin Hood

B. 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 hasci coins. Each day, Robin Hood will take exactly1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's1 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 ink 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 afterk 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 integersn andk (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, thei-th of them isci (1 ≤ ci ≤ 109) — initial wealth of thei-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.


        题意:有一个大盗,偷k天,每天去最富的人那里劫取 1$ 给了最穷的人,富人也可能变成穷人,问k天后最富的人和最穷的人的钱差距多大。



思路:①从小到大排序

           ②分别从左到右、从右到左扫一遍,用两个数组存下要前i-1和i有一样的钱(要后n-1-j和j有一样的钱)需要多给

               (被偷)多少钱

           ③分别从左到右、从右到左再扫一遍,找出小于k且最接近k的i和j (确定k天能涉及或者说改动哪些人的资产)

           ④分三种情况:

                    1.(→   ←)i-1和j+1没相遇:不会涉及所有人,最小值为改变(见下)后的a[i-1],最大值为改变后的a[j+1]

                        1)改变:将k分给(加上)前i-1个人,能平分就平分,不能平分时优先给前面(小)的;将k分给(减去)后

                                         n-1-j个人,能平分就平分,不能平分时优先减后面(大)的。

                    2.(←→)i-1和j+1交错:涉及所有人,穷人先变成富人再变穷,能平分差0,不能平分差1

                    3.(→←)i-1和j+1正好相遇:涉及所有人,可能大的正好补全小的(或差一)同2,也可能情况同1,这

                    里不做单独判断,12都进行一遍取最大。



代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int const MAX = 500000 + 5;
long long a[MAX],pr[MAX],en[MAX],b[MAX];
int main()
{
    int n,k,i,j;
    scanf("%d%d",&n,&k);
    for(i = 0; i < n; i++)
        scanf("%d",&a[i]);
    sort(a,a+n);


    pr[0] = 0;                                            // ②
    long long sum = a[0];
    for(i = 1; i < n; i++)
    {
        pr[i] = a[i]*i - sum;
        sum += a[i];
    }
    en[n-1] = 0;
    sum = a[n-1];
    for(i = n-2; i >= 0; i--)
    {
        en[i] = sum - (n - i-1)*a[i];
        sum += a[i];
    }


    for(i = 1; i < n; i++)                              // ③
        if(pr[i] > k)
            break;
    for(j = n - 1; j >= 0; j--)
        if(en[j] > k)
            break;



    if(i - j > 1)                                     //④:2
    {
        if(sum%n == 0)
            printf("0");
        else
            printf("1");
        return 0;
    }


    long long Min,Max,d = k - pr[i - 1];              // ④:13结合
    if(d == 0)
        Min = a[i - 1];
    else
    {
        d = d/i;
        a[i - 1] += d;
        Min = a[i - 1];
    }
    d = k - en[j + 1];
    if(d == 0)
        Max = a[j + 1];
    else
    {
        if(d<0)
            d = -d;
        d = d/(n - 1 - j);
        a[j + 1] -= d;
        Max = a[j + 1];
    }
    sum = (sum%n==0?0:1);                           // ④:3用来判断正好相遇时的1情况
    printf("%d",max(Max - Min,sum));
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值