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. 这题网上有很多题解,我自己是很难做出来的 ...
随机推荐
- Spring boot 配置异步处理执行器
示例如下: 1. 新建Maven 项目 async-executor 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
- tomcat - 自带日志的区分
在tomcat 中,logs文件夹下会存放着一些tomcat自带的日志文件,其中有三种文件: 1 > localhost_access_log.2017-12-28 文件,它用来记录tomcat ...
- 判断用户ip是否在指定的一个ip段内
/** * 判断ip是否在一个ip段内 * * @param args */ public static boolean ipExistsInRange(String ip, String ipSec ...
- bug汇总
bug 2018年8月23日 bug 1:散点图画不出来. plt.scatter(validation_examples["longitude"], validation_exa ...
- PHP计算今天、昨天、本周、本月、上月开始时间和结束时间
PHP计算今天.昨天.本周.本月.上月开始时间和结束时间 $today = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d'),date('Y')) ...
- Ubuntu 18.04 下用命令行安装Sublime
介绍: 添加来源: $ wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add - $ sud ...
- 通过代码链接ftp上传下载删除文件
因为我的项目是Maven项目,首先要导入一个Maven库里的包:pom.xml <dependency> <groupId>com.jcraft</ ...
- loadView、viewDidLoad及viewDidUnload的关系(转)
本文目录 一.loadView 二.viewDidLoad 三.viewDidUnload 四.三个方法的关系 标题中所说的3个方法,都是UIViewController的方法,跟UIViewCont ...
- CodeForces:148D-D.Bag of mice
Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes Program Description Th ...
- Setting title-center on "< h1> " element on Android does not work, fix
app.scss: h1.title-center{ text-align: center!important; }