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& ...
随机推荐
- 如果jsp提交到action为空指针的话
很严重的一点:表单<form>有没有添加一个method="post",如果表单的这个没有写,肯定是空指针, 哎,被这个坑爹的代码,查了好久,特此记录下
- WPF MVVM模式
1. MVVM MVVM的设计模式最早于2005年由微软的WPF和Silverlight架构师John Gossman在他的博客中提到. WPF中采用MVVM的架构可以获得以下好处: 1. 将UI和业 ...
- object-c(oc)内存管理机制详解
1.内存的创建和释放 让我们以Object-c世界中最最简单的申请内存方式展开,谈谈关于一个对象的生命周期.首先创建一个对象: 1 2 3 //“ClassName”是任何你想写的类名,比如NSStr ...
- dns (域名系统)
dns (域名系统) DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP ...
- Quartz.net开源作业调度框架使用详解(转)
前言 quartz.net作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本.支持 cron-like表达式,集群,数据库.功能性能强大更不 ...
- 漫长Appium之路(二)——Appium安装与使用总结
前面介绍了iOS自动化工具的Appium所需的虚拟机环境,接下来介绍下Appium的安装与使用方法,这个足足折腾我将近一个星期.网上没有什么详细的资料,对于遇到的各种各样问题也没用提供明确的解决方法. ...
- Matlab之字符串处理
Matlab处理字符串 1.取得部分字符串 我们有一个字符串 file='20131030_113109.TemporaryAlias.Poly5'; 简单操作举例: >> a=file( ...
- CocoStudio基础教程(6)使用CocoStudio编辑帧事件并关联到程序
1.概述 帧事件也是新加入的功能.这篇中我们将看到如何使用它.我们将上篇中制作的动画稍加修改. 2.用途与原理 首先介绍一下帧事件.正如其名:一个与帧相关联的事件. 为什么要这么做呢?首先没人想做一大 ...
- [Effective JavaScript 笔记]第19条:熟练掌握高阶函数
高阶函数介绍 高阶函数曾经是函数式编程的一个概念,感觉是很高深的术语.但开发简洁优雅的函数可以使代码更加简单明了.过去几年中脚本语言采用了这些个技术,揭开了函数式编程的最佳惯用法的神秘面纱.高阶函数就 ...
- 5 个最受人喜爱的开源 Django 包
导读 Django 围绕“可重用应用”的思想建立:自包含的包提供了可重复使用的特性.你可以将这些可重用应用组装起来,在加上适用于你的网站的特定代码,来搭建你自己的网站.Django 具有一个丰富多样的 ...