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++. ...
随机推荐
- 文件操作---基于python
# coding:utf-8from time import sleepimport sysreload(sys)sys.setdefaultencoding("utf8")f=o ...
- Java接口与继承作业
为什么子类的构造方法在运行之前,必须调用父类的构造方法?能不能反过来?为什么不能反过来? 因为子类继承了父类,那么就默认的含有父类的公共成员方法和公共成员变量,这些方法和变量在子类里不再重复声明.如果 ...
- http-bio-8080"-exec-6"(转)
现象如下: Tomcat7启动后,后台抛出如下异常,前台一直无法登陆Exception in thread ""http-bio-8080"-exec-6" j ...
- 【android】实现手指滑动来切换activity(转)
http://code.eoe.cn/115 1.jpg外部引用 原始文档 MainActivity.java外部引用 原始文档 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- PCA算法理解及代码实现
github:PCA代码实现.PCA应用 本文算法均使用python3实现 1. 数据降维 在实际生产生活中,我们所获得的数据集在特征上往往具有很高的维度,对高维度的数据进行处理时消耗的时间很大, ...
- Swift-函数的理解
/* 函数(Function) 函数是为执行特定功能的自包含的代码块.函数需要给定一个特定标识符(名字),然后当需要的时候, 就调用此函数来执行功能. */ // 函数的定义与调用 // 定义函数时, ...
- vim map nmap(转)
转自:http://blog.csdn.net/taoshengyang/article/details/6319106 有五种映射存在 - 用于普通模式: 输入命令时. - 用于可视模式: 可视 ...
- project之chrome.exe
查看chrome.exe的以来文件可以得到下面这个列面,大部分是在%systemroot%/system32下面的系统dll文件,只有两个是chromium自己生成的:base.dll, conten ...
- Spring MVC 之@Controller@RequestMapping详解
一:配置web.xml 1)问题:spring项目中有多个配置文件mvc.xml dao.xml 2)解决:在web.xml中 <init-param> <param-name& ...
- query 获取本身的HTML
<div class="test"><p>hello,你好!</p></div> <script> $(".t ...