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, 4, 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.
===================EDITORIAL=================
We observe that we can apply operations separately this means first we will apply all increase operations, and after will apply all decrease operations, vice versa. We will use following algorithm.
We have to apply following operation k times, add one to minimum number in array. We will simply binary search over minimum value after applying k operation. Let's look how to check if minimum will be great or equal to p. If
is less or equal to kthen we can increase all elements to at least p. In this way we can find what will be minimum value after k operations.
Let's say this minimum value to m. After we find it we will increase all numbers less than m to m. But there may be some operations we still didn't apply yet, this number can be at most n - 1, otherwise minimum would be greater than m because we can increase all numbers equal to m by one with this number of increases. Since minimum has to be same we just have to increase some elements in our array that equal to m.
We will use same algorithm for decreasing operations. Finally we will print max element — min element in final array. Overall complexity will be O(nlogk).
CORRECTNESS PROOF
this is quit simple to prove that both increase operations ans decrease operations can be done separately , since we have K operations , we can use these all these k operation ( while decreasing ) on any element without thinking about which element will be added with this value , since which ever element will be added with this value will remain less than the mid( value in the b search ) ,
similarly for the increase operation we can operate without taking care about of number which will decrease ...
=========================CODE===========================================
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int arr[1000000];
int maxi;
int mini;
int n,k;
void set_min()
{
mini=arr[0];
// cout<<" initial min"<<mini<<endl;
int lo=mini;
int hi=maxi;
int f=0;
while(lo<=hi)
{
if(lo==hi)
{
f=1;
}
int mid=(lo+hi)/2;
lli ext=0;
lli less=0;
for(int i=0;i<n;i++)
{
if(arr[i]>mid)
{
ext+=arr[i]-mid;
}
else
{
less+=mid-arr[i];
}
}
if(less<=k && less<=ext)
{
lo=mid+1;
mini=mid;
}
else
{
hi=mid;
}
if(f==1) break;
}
// cout<<" final min "<<mini<<endl;
}
void set_max()
{
maxi=arr[n-1];
//cout<<" initial max "<<maxi<<endl;
int lo=0;
int hi=maxi;
int f=0;
while(lo<=hi)
{
if(lo==hi)
{
f=1;
}
int mid=(lo+hi)/2;
lli ext=0;
lli less=0;
for(int i=0;i<n;i++)
{
if(arr[i]>mid)
{
ext+=arr[i]-mid;
}
else
{
less+=mid-arr[i];
}
}
// cout<<" mid "<<mid<<" "<<ext<<" "<<less<<endl;
if(ext<=k && less>=ext)
{
maxi=mid;
hi=mid;
}
else
{
lo=mid+1;
}
if(f==1) break;
}
//cout<<" final max "<<maxi<<endl;
}
int main()
{
cin>>n>>k;
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
sort(arr,arr+n);
set_max();
set_min();
cout<<maxi-mini<<endl;
}
No comments:
Post a Comment