Codeforces 671B/Round #352(div.2) 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.
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.
题意:给你n个数,每次能从最大的数一个移给最小的数,问k次操作后,最大和最小的数的差。
思路 : 题目可以简化成找最终状态下的最大值和最小值,那么分别进行二分最大值和最小值,此外注意二分最大、最小时的不同之处。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std; const int MAX = ;
const int INF = 0x3f3f3f3f;
int a[MAX], n; int findmax(int x, int k)
{
for(int i = ; i < n; i++)
{
if(a[i] > x)
k-=a[i] - x;
if(k < )
return ;
}
return ;
}
int findmin(int x, int k)
{
for(int i = ; i < n; i++)
{
if(a[i] < x)
k-=x - a[i];
if(k < )
return ;
}
return ;
} int main()
{
int k;
__int64 sum = ;
int t1, t2;
cin >> n >> k;
t1 = ;
t2 = INF;
for(int i = ; i < n; i++)
{
scanf("%d", a + i);
sum += a[i];
t1 = max(t1, a[i]);
t2 = min(t2, a[i]);
}
int mx = sum / n + (sum%n> ? : );
int mi = sum / n; int l , r, maxx , minn;
/////mi
l = t2, r = mi;
while(l <= r) //注意等于
{
int mid = (l + r) >> ;
if(findmin(mid, k))
{
l = mid + ;
minn = mid;
}
else r = mid - ;
}
////ma
l = mx, r = t1;
while(l < r)
{
int mid = (l + r) >> ;
if(findmax(mid, k))
{
r = mid;
maxx = max(mid, maxx);
}
else l = mid + ;
}
maxx = (l + r) >>; //结束状态时取
printf("%d\n", maxx - minn);
}
Codeforces 671B/Round #352(div.2) D.Robin Hood 二分的更多相关文章
- 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 Round #352 (Div. 2) D. Robin Hood (二分答案)
题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...
- 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 Round #352 (Div. 1) B. Robin Hood
B. Robin Hood 讲道理:这种题我是绝对不去(敢)碰的.比赛时被这个题坑了一把,对于我这种不A不罢休的人来说就算看题解也要得到一个Accepted. 这题网上有很多题解,我自己是很难做出来的 ...
- Codeforces Round #352 (Div. 2) D. Robin Hood
题目链接: http://codeforces.com/contest/672/problem/D 题意: 给你一个数组,每次操作,最大数减一,最小数加一,如果最大数减一之后比最小数加一之后要小,则取 ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
随机推荐
- HTML5 Geolocation位置信息定位总结
现在定位功能很常用,所以抽出一些时间将这个功能的知识总结一下作为知识梳理的依据.HTML5 Geolocation的定位用法很简单,首先请求位置信息,用户同意,则返回位置信息.HTML5 Geoloc ...
- 自测之Lesson3:makefile
题目:编写一个makefile文件,要求编译当前目录内的所有.c文件. 完成代码: .PHONY:clean all SRC=$(wildcard *.c) BIN=$(SRC:%.c=%) all: ...
- Scrum1
Scrum1 组员 任务分工 贡献 林泽宇 团队分工.撰写博客.修改完善需求规格说明书.整理代码规范 李涵 后端架构设计 尹海川 logo设计修改.数据库数据 郏敏杰 课堂展示.查阅资料.整理关键和难 ...
- Ubuntu 配置 Android 开发 环境
. 果断换Ubuntu了, Ubuntu的截图效果不好, 不能设置阴影 ... 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article ...
- sping框架(3)— 使用spring容器
spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.它们都可以代表spring容器,sp ...
- lintcode-184-最大数
184-最大数 给出一组非负整数,重新排列他们的顺序把他们组成一个最大的整数. 注意事项 最后的结果可能很大,所以我们返回一个字符串来代替这个整数. #### 样例 给出 [1, 20, 23, 4, ...
- IOS开发NSBundle使用
bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBu ...
- centos7 下pycharm无法输入中文问题解决方案
作者使用的pycharm是2017.2 在pycharm.sh脚本的如下行(大约在201行): # -------------------------------------------------- ...
- C#的垃圾回收
C#中垃圾回收 GC.Collect();强制进行内存回收.
- Delphi中Sender对象的知识
Sender是一个TObject类型的参数,它告诉Delphi哪个控件接收这个事件并调用相应的处理过程.你可以编写一个单一的事件处理句柄,通过Sender参数和IF…THEN…语句或者CASE语句配合 ...