codeforces 672D D. Robin Hood(二分)
题目链接:
1 second
256 megabytes
standard input
standard 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.
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.
Print a single line containing the difference between richest and poorest peoples wealth.
4 1
1 1 4 2
2
3 1
2 2 2
0
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.
题意:
每天把最大数减1,最小的数加1,问k天之后最大的数和最小的数的差是多少;
思路:
分别二分k天之后的最大的数和最小的数,相减就是结果了,不过要先求出这两个二分的上下限,具体的看代码;
AC代码:
#include <bits/stdc++.h>
/*#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=5e5+;
int a[N];
int n,k;
int cmp(int x,int y)
{
return x>y;
}
int check(int x)
{
LL sum=;
for(int i=;i<=n;i++)
{
if(a[i]>x)
{
sum+=(LL)(a[i]-x);
}
else break;
}
if(sum>k)return ;
return ;
}
int ok(int x)
{
LL sum=;
for(int i=n;i>;i--)
{
if(a[i]<x)
{
sum+=(LL)(x-a[i]);
}
else break;
}
if(sum>k)return ;
return ;
}
int main()
{
int ans;
LL s=;
scanf("%d%d",&n,&k);
Riep(n)scanf("%d",&a[i]),s+=(LL)a[i];
int mmin,mmax;//mmin最大的数的下限,mmax为最小数的上限;
if(s%n==)
{
mmin=mmax=s/n;
}
else
{
mmin=s/n;
mmax=mmin+;
}
sort(a+,a+n+,cmp);
int l=mmax,r=1e9+;
while(l<=r)
{
int mid=(l+r)>>;
if(!check(mid))l=mid+;
else r=mid-;
}
ans=l;
l=,r=mmin;
while(l<=r)
{
int mid=(l+r)>>;
if(ok(mid))l=mid+;
else r=mid-;
}
ans=ans-r;
cout<<ans<<"\n";
return ;
}
codeforces 672D D. Robin Hood(二分)的更多相关文章
- 【15.93%】【codeforces 672D】Robin Hood
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 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 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 ...
- 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. 2) D. Robin Hood (二分答案)
题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...
- codeforces 671B Robin Hood 二分
题意:有n个人,每个人a[i]个物品,进行k次操作,每次都从最富有的人手里拿走一个物品给最穷的人 问k次操作以后,物品最多的人和物品最少的人相差几个物品 分析:如果次数足够多的话,最后的肯定在平均值上 ...
- Codeforces 672D Robin Hood(二分好题)
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- poj2513字典树+欧拉图判断+并查集断连通
题意:俩头带有颜色的木棒,要求按颜色同的首尾相连,可能否? 思路:棒子本身是一条边,以俩端为顶点(同颜色共点),即求是否有无向图欧拉路(每条棒子只有一根, 边只能用一次,用一次边即选一次棒子). 先判 ...
- ngxtop
http://www.cnblogs.com/felixzh/p/8709201.html
- 提高google网站访问速度
修改:C:\Windows\System32\drivers\etc\hosts文件 # google websites.203.208.46.180 ssl.gstatic.com203.208.4 ...
- ganglia-monitoring-centos-linux
http://www.tecmint.com/install-configure-ganglia-monitoring-centos-linux/ http://monitor.gitlab.net/ ...
- 深入GCD(二): 多核心的性能
概念为了在单一进程中充分发挥多核的优势,我们有必要使用多线程技术(我们没必要去提多进程,这玩意儿和GCD没关系).在低层,GCD全局dispatch queue仅仅是工作线程池的抽象.这些队列中的Bl ...
- mysql修改删除列,删除有外键依赖的列
–重命名表rename table t_softwareport to software_port; –建立外键alter table software_port add constraint fk_ ...
- 重装系统(Win)
有朋友问我,重装系统该怎样操作呢? 1. 硬盘重装 官网:http://www.heiyunwang.com/ ,点击下载软件:http://dlsw.baidu.com/sw-search-sp/s ...
- ``Accordian'' Patience
``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patience, the rule ...
- HDU 5289 Assignment(多校联合第一场1002)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- VS2013带来的"新特性"
VS2013除了引入"Bootstrap"库以外,最大的变化就是.net Framework 4.5下面的都不支持了.也就是说,假设不把.net库升级成.net Framework ...