首页 > 学院 > 开发设计 > 正文

Codeforces Round #352 (Div. 2) D

2019-11-06 07:21:50
字体:
来源:转载
供稿:网友

D. Robin Hood time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output 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.

#include <stdio.h>#include <algorithm>#define LL long longusing namespace std;const int N=500005;LL c[N];LL n,m;int ok_mins(LL k){ LL sum=0; for(int i=0;i<n;i++){ if(c[i]<k){ sum+=(k-c[i]); } else{ break; } } if(sum<=m) return 1; else return 0;}int ok_maxs(LL k){ LL sum=0; for(int i=n-1;i>=0;i--){ if(c[i]>k) sum+=(c[i]-k); else break; } if(sum<=m) return 1; else return 0;}int main(){ LL sum=0; scanf("%I64d%I64d",&n,&m); for(int i=0;i<n;i++) { scanf("%I64d",&c[i]); sum+=c[i]; } sort(c,c+n); LL up,down; if(sum%n==0) up=down=sum/n; else{ up=sum/n; down=sum/n+1; } LL l=0,r=up; LL mins=0; while(l<=r){ LL mid=(l+r)/2; if(ok_mins(mid)){ mins=mid; l=mid+1; } else{ r=mid-1; } } l=down,r=1e15+0; LL maxs=0; while(l<=r){ LL mid=(l+r)/2; if(ok_maxs(mid)){ maxs=mid; r=mid-1; } else{ l=mid+1; } } printf("%I64d",maxs-mins); return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表