cf671B 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 11 1 4 2

Output

2

Input

3 12 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整除讨论

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<deque>
 9 #include<set>
10 #include<map>
11 #include<ctime>
12 #define LL long long
13 #define inf 0x7ffffff
14 #define pa pair<int,int>
15 #define mkp(a,b) make_pair(a,b)
16 #define pi 3.1415926535897932384626433832795028841971
17 using namespace std;
18 inline LL read()
19 {
20     LL x=0,f=1;char ch=getchar();
21     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
22     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
23     return x*f;
24 }
25 int n,k,ave,mx;
26 int a[500010];
27 LL s[500010];
28 int lim1,lim2,lim3;
29 inline bool jud(int d,int op)
30 {
31     LL sum=0;
32     if (op==1)
33     {
34         for (int i=1;i<=lim1;i++)if (a[i]<d)sum+=d-a[i];else break;
35         return sum<=k;
36     }else
37     {
38         for (int i=n;i>=lim2;i--)if (a[i]>d)sum+=a[i]-d;else break;
39         return sum<=k;
40     }
41 }
42 int main()
43 {
44     while (~scanf("%d%d",&n,&k))
45     {
46         mx=-1;
47         for (int i=1;i<=n;i++)a[i]=read(),s[i]=s[i-1]+a[i],mx=max(mx,a[i]);
48         sort(a+1,a+n+1);
49         ave=s[n]/n;
50         lim1=1;lim2=n;
51         if ((LL)ave*n==s[n])
52         {
53             for (int i=1;i<=n;i++)
54                 if (a[i]<ave)lim1=i;else break;
55             for (int i=n;i>=1;i--)
56                 if (a[i]>ave)lim2=i;else break;
57         }else
58         {
59             for (int i=1;i<=n;i++)
60                 if (a[i]<=ave)lim1=i;else break;
61             for (int i=n;i>=1;i--)
62                 if (a[i]>=ave+1)lim2=i;else break;
63         }
64         int L=ave,R=ave;
65         int l=0,r=ave;
66         while (l<=r)
67         {
68             int mid=(l+r)>>1;
69             if (jud(mid,1))L=mid,l=mid+1;
70             else r=mid-1;
71         }
72         l=((LL)ave*n==s[n])?ave:ave+1;r=mx;
73         while (l<=r)
74         {
75             int mid=(l+r)>>1;
76             if (jud(mid,2))R=mid,r=mid-1;
77             else l=mid+1;
78         }
79         printf("%d\n",R-L);
80     }
81 }

cf671B

时间: 2024-11-05 16:23:16

cf671B Robin Hood的相关文章

Curious Robin Hood(树状数组+线段树)

1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another tri

Codeforces Round #352 (Div. 2) D. Robin Hood

题目连接请戳此处 题意:n个人每人有一定的财富值ci,每天Robin Hood从最富的人手中取财富1分给最穷的人(取后最穷, 即可以退回),若有多个最穷的人,任选一个给出财富值.问k天后最富的人和最穷的人财富差值为多少. 1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109 1 ≤ ci ≤ 109 分析: 每一天随着财富值的取和给,最穷的人和最富的人都在动态更新. 最后要求的是  (richest-poorest)min,那么  要使这个等式最小,只需求出k天后richest的最小值和po

LightOj 1112 Curious Robin Hood(线段树||树状数组)

Curious Robin Hood Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps n sacks where he keeps this money. The sacks are numbered from 0 to n-1.

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

Lightoj 1112 - Curious Robin Hood 【单点改动 + 单点、 区间查询】【树状数组 水题】

1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick.

Lightoj 1112 - Curious Robin Hood 【单点修改 + 单点、 区间查询】【树状数组 水题】

1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick.

Light oj 1112 - Curious Robin Hood【单点更新】

1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another tri

CodeForces 672D Robin Hood

思维. 当$k$趋向于正无穷时,答案会呈现出两种情况,不是$0$就是$1$.我们可以先判断掉答案为$1$和$0$的情况,剩下的情况都需要计算. 需要计算的就是,将最小的几个数总共加$k$次,最小值最大会是多少,以及将最大的几个数总共减$k$次,最大值最小可能是多少.两者相减就是答案. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring>

Codeforces Round #352 (Div. 2) D. Robin Hood (二分法+判断平衡态)

解题思路: 由求最小值和求最大值各自二分. 由l*(a[l+1]-a[l]):求取填 1~l 到a[l+1]的高度需要多少的钱,如果大于剩余的k 则可执行 若否 判断剩余的k是否为l,若否最小值为a[l],否则 为a[l]+k/l; 由(n-r+1)*(a[r]-a[r-1]):求去掉n~n-r+1的这部分需要多少钱,如果大于剩余的k 则执行 若否 判断剩余的k是否为n-r+1,若是最大值为a[n-r+1]-k/(n-r+1); 如果最大值小于最大值,直接printf 如果最小值大于等于最大值,