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
4 1
1 1 4 2
2
3 1
2 2 2
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.
先排个序、算个平均数,然后左右二分,看看左右能到达的最接近平均的位置在哪
蒟蒻写的比较挫,还要分总和能不能被n整除讨论
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,k,ave,mx;
int a[];
LL s[];
int lim1,lim2,lim3;
inline bool jud(int d,int op)
{
LL sum=;
if (op==)
{
for (int i=;i<=lim1;i++)if (a[i]<d)sum+=d-a[i];else break;
return sum<=k;
}else
{
for (int i=n;i>=lim2;i--)if (a[i]>d)sum+=a[i]-d;else break;
return sum<=k;
}
}
int main()
{
while (~scanf("%d%d",&n,&k))
{
mx=-;
for (int i=;i<=n;i++)a[i]=read(),s[i]=s[i-]+a[i],mx=max(mx,a[i]);
sort(a+,a+n+);
ave=s[n]/n;
lim1=;lim2=n;
if ((LL)ave*n==s[n])
{
for (int i=;i<=n;i++)
if (a[i]<ave)lim1=i;else break;
for (int i=n;i>=;i--)
if (a[i]>ave)lim2=i;else break;
}else
{
for (int i=;i<=n;i++)
if (a[i]<=ave)lim1=i;else break;
for (int i=n;i>=;i--)
if (a[i]>=ave+)lim2=i;else break;
}
int L=ave,R=ave;
int l=,r=ave;
while (l<=r)
{
int mid=(l+r)>>;
if (jud(mid,))L=mid,l=mid+;
else r=mid-;
}
l=((LL)ave*n==s[n])?ave:ave+;r=mx;
while (l<=r)
{
int mid=(l+r)>>;
if (jud(mid,))R=mid,r=mid-;
else l=mid+;
}
printf("%d\n",R-L);
}
}
cf671B
cf671B Robin Hood的更多相关文章
- Codeforces 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 a ...
- Curious Robin Hood(树状数组+线段树)
1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 ...
- CF 672D Robin Hood(二分答案)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #352 (Div. 1) B. Robin Hood 二分
B. Robin Hood 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...
- 【CodeForces】671 B. Robin Hood
[题目]B. Robin Hood [题意]给定n个数字的序列和k次操作,每次将序列中最大的数-1,然后将序列中最小的数+1,求最终序列极差.n<=5*10^5,0<=k<=10^9 ...
- 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 ...
- Codeforces Round #352 (Div. 1) B. Robin Hood (二分)
B. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 672D Robin Hood(二分好题)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #352 (Div. 1) B. Robin Hood
B. Robin Hood 讲道理:这种题我是绝对不去(敢)碰的.比赛时被这个题坑了一把,对于我这种不A不罢休的人来说就算看题解也要得到一个Accepted. 这题网上有很多题解,我自己是很难做出来的 ...
随机推荐
- 为DataGridView控件实现复选功能
实现效果: 知识运用: DataGridViewCheckBoxColumn类 实现代码: private class Fruit { public int Price { get; set; } p ...
- AutoLayout处理UITableView动态高度
我们经常会遇到UITableViewCell的高度要跟随内容而调整,在未引入AutoLayout之前,我们使用以下方法计算Label高度,然后heightForRowAtIndexPath中返回计算的 ...
- 八皇后问题(DFS)
题目描述: 要在国际象棋棋盘中放八个皇后,使任意两个皇后都不能互相吃,皇后能吃同一行.同一列,同一对角线上(两个方向的对角线)的任意棋子.现在给一个整数n(n<=92),输出前n种的摆法. 输入 ...
- linux文件属性之用户和组基础知识
root :x :0 :0 :root ...
- 中移物联网onenet入门学习笔记2:中移物联的通信格式
中移物联网硬件接入协议:LWM2M协议,EDP协议,MQTT协议,HTTP协议,TCP透传,MODBUS协议,JT/T808协议,RCMP协议 8种通信协议的区别在哪? 详细比较 EDP:OneNET ...
- vagrant中的precise64使用静态的能和host所在局域网的其他机器互相通信
vagrant中的precise64使用静态的能和host所在局域网的其他机器互相通信
- 关于51单片机IO引脚的驱动能力与上拉电阻
单片机的引脚,可以用程序来控制,输出高.低电平,这些可算是单片机的输出电压.但是,程序控制不了单片机的输出电流. 单片机的输出电流,很大程度上是取决于引脚上的外接器件. 单片机输出低电平时,将允许外部 ...
- CSS动画小结
CSS动画 原理:1.画面之间变化 2.视觉暂留作用 常见问题 1.CSS 动画的实现方式有几种 1.transition 2. keyframes(animation) 2.过渡动画和关键帧动画 ...
- exe4j+Inno_setup打包java桌面应用
打开exe4j,这里有个注意点,就是欢迎界面下面的License,如果没有请到网上找一个序列号,否则生成的exe打开之后都会先弹出您未激活exe4j的警告!点击下一步 这里有两个选项,第一个是通常编译 ...
- Django one
WEB-Django: Http协议: http协议:超文本传输协议,基于TCP/IP通信协议来传递数据 特点: 1.灵活:允许传输任意类型的数据对象.正在传输的类型有Content-Type标记 2 ...