Codeforces 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
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天下
我们二分最大值尽可能的 小,最小值最可能的大就可以了
#include<bits/stdc++.h>
using namespace std;
const int N = 3e6+, M = 1e7, mod = 1e9+,inf = 1e9+;
typedef long long ll; int a[N],n,k;
int check1(int mx) {
ll kk = ,buji = ;
for(int i=;i<=n;i++) {
if(a[i]>mx) kk+=(a[i]-mx);
else {
buji+=(mx - a[i]);
}
}
if(kk<=k&&buji>=kk) return ;
else return ;
}
int check2(int mi) {
ll kk = ,buji = ;
for(int i=;i<=n;i++) {
if(a[i]<mi) kk+=(mi - a[i]);
else buji += (a[i] - mi);
}
if(kk<=k&&buji>=kk) return ;
else
return ;
}
int main() {
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a+n+);
int mx = a[n],mi = a[];
int r = a[n], l = a[];
while(l<=r) {
int mid = (l+r)>>;
if(check1(mid)) r = mid-,mx = mid;
else l = mid+;
}
r = a[n], l = a[];
while(l<=r) {
int mid = (l+r)>>;
if(check2(mid)) l = mid+, mi = mid;
else r = mid-;
}
//cout<<mx<<" "<<mi<<endl;
cout<<mx - mi<<endl;
return ;
}
Codeforces 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 (二分答案)
题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...
- 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 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 Round #352 (Div. 2) ABCD
Problems # Name A Summer Camp standard input/output 1 s, 256 MB x3197 B Different is Good ...
- Codeforces Round #352 (Div. 2)
模拟 A - Summer Camp #include <bits/stdc++.h> int a[1100]; int b[100]; int len; void init() { in ...
- Codeforces Round #352 (Div. 2) (A-D)
672A Summer Camp 题意: 1-n数字连成一个字符串, 给定n , 输出字符串的第n个字符.n 很小, 可以直接暴力. Code: #include <bits/stdc++.h& ...
随机推荐
- 百度或者Google---SEO优化(转载)
google 和百度的技术差别: 1.百度还认不清哪个是原创的 2.google蜘蛛不够百度快 4.google排名结果随时变化 流量.权重.权威.内容.用户体验.用户关注度等等细节的排名,已表 达了 ...
- Ioc注解
注解: 添加注解时,需要添加context的相关 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- C#多线程学习 之 线程池[ThreadPool](转)
在多线程的程序中,经常会出现两种情况: 一种情况: 应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应 这一般使用ThreadPo ...
- OCJP-试题集合 | 对象的比较
Boolean b1 = new Boolean(true); Boolean b2 = new Boolean(true); 下面哪个能得到true的结果: A b1 == b2 B b1.equa ...
- 查看远程git log
$ git reset --hard bit/add $ git log --oneline --graph
- linux expect 简单讲解
来自http://blog.csdn.net/winstary/archive/2009/08/08/4422156.aspx使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明, ...
- 中位数与第K小元素
算法实际上是模仿快速排序算法设计出来的,其基本思想也是对输入数组进行递归划分,与快速排序不同的是,它只对划分出来的子数组之一进行递归处理: int randompartition(int a[],in ...
- 《ASP.NET1200例》C# WINFORM程序的三层架构如何建立的。
先添加-新建项目-windows应用程序,然后在右边的解决方案资源管理器上面,在当前的解决方案上面右击,点,添加-新建项目-类库,分别建立.DAL,BLL,Model三个项目,然后,在DAL项目上右击 ...
- Continuous Subarray Sum
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- shell脚本去重的几种方法
测试文件 [root@bogon ~]# cat >test jason jason jason fffffjason 按 Ctr + D保存 1.sort -u [root@bogon ~]# ...