Codeforces672D(SummerTrainingDay01-I)
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.
Input
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.
Output
Print a single line containing the difference between richest and poorest peoples wealth.
Examples
input
4 1
1 1 4 2
output
2
input
3 1
2 2 2
output
0
Note
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天之后最富有的人跟最少财富的人的差值是多少。
思路:二分最后一天时的最大值和最小值。avg为数组平均值,最大值在avg到MAX之间,最小值在0到avg之间。check条件为能否用k使得小于mid的数都变为mid。
//2017-10-15
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
int arr[N]; int main()
{
//freopen("inputI.txt", "r", stdin);
std::ios::sync_with_stdio(false);
cin.tie();
int n, k;
while(cin>>n>>k){
long long sum = ;
int MAX = ;
for(int i = ; i < n; i++){
cin>>arr[i];
MAX = max(MAX, arr[i]);
sum += arr[i];
}
sort(arr, arr+n);
int L = sum/n, R = (sum+n-)/n;
int l = , r = L, mininum = ;
while(l <= r){
int mid = (l+r)>>;
long long tmp = ;
for(int i = ; i < n; i++)
if(arr[i] < mid)
tmp += mid-arr[i];
else break;
if(tmp <= k){
mininum = mid;
l = mid+;
}else r = mid-;
}
l = R, r = MAX;
int maxinum = ;
while(l <= r){
int mid = (l+r)>>;
long long tmp = ;
int pos = lower_bound(arr, arr+n, mid)-arr;
for(int i = pos; i < n; i++)
if(arr[i] > mid)
tmp += arr[i]-mid;
if(tmp <= k){
maxinum = mid;
r = mid-;
}else l = mid+;
}
cout<<maxinum-mininum<<endl;
} return ;
}
Codeforces672D(SummerTrainingDay01-I)的更多相关文章
- 二分算法题目训练(四)——Robin Hood详解
codeforces672D——Robin Hood详解 Robin Hood 问题描述(google翻译) 我们都知道罗宾汉令人印象深刻的故事.罗宾汉利用他的射箭技巧和他的智慧从富人那里偷钱,然后把 ...
随机推荐
- 注册Docker官网账号 注册按钮不能点
出现如下问题:注册按钮不能点,解决办法,如下 关于docker hub上不能注册dockeID的问题 注意的是,google访问助手,用在线安装,360安全浏览器,再重启下该浏览器,省得装插件.
- String str.trim()
String.trim() 方法不仅仅是去除字符串两端的空格字符,它能去除25种字符: ('/t', '/n', '/v', '/f', '/r', ' ', '/x0085', '/x00a0', ...
- 了解Java基本数据类型的取值范围
拿byte类型做栗子 一个字节是8位二进制数,然后最高位会用来作为符号位.正数计算机是存的原码,负数是存的补码. 也就说byte正数最大是0111 1111,转化为十进制是:127(这就是byte的上 ...
- 《分布式Java应用与实践》—— 后面两章
failover? NAT IP-tunneling DSR vrrp gossip 什么是2PC? 什么是3PC? 什么是Pasox? sna? dal? mpi?
- weblogic安装及配置
WebLogic是用于开发.集成.部署和管理大型分布式Web应用.网络应用和数据库应用的Java应用服务器. 1.安装Weblogic:(1)点击Next按钮:(2) 选择Custom后点击Next按 ...
- Docker优势
设计,开发 ---> 测试 ----> 部署,运行 代码+运行环境 ---> 镜像 image 环境一致,资源占用少 自动化平台 Docker image的制作很重要
- Maven - 实例-4-依赖传递
这里以Eclipse创建Maven工程来演示. Setp-1 创建Maven项目 File ---> New ---> Maven Project ---> 默认勾选"Us ...
- 阿里P9架构师讲解从单机至亿级流量大型网站系统架构的演进过程
阶段一.单机构建网站 网站的初期,我们经常会在单机上跑我们所有的程序和软件.此时我们使用一个容器,如tomcat.jetty.jboos,然后直接使用JSP/servlet技术,或者使用一些开源的框架 ...
- jenkins内部分享ppt
持续集成Continuous integration简介(持续集成是什么) .持续集成源于极限编程(XP),是一种软件实践,软件开发过程中集成步骤是一个漫长并且无法预测的过程.集成过程中可能会爆 ...
- SQL Server性能优化(11)非聚集索引的覆盖索引存储结构
一,非聚集索引的include 非聚集索引的Include属性可以让非聚集索引包含其他列.如 CREATE NONCLUSTERED INDEX [NonIxUser] ON [dbo].[Users ...