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翻译) 我们都知道罗宾汉令人印象深刻的故事.罗宾汉利用他的射箭技巧和他的智慧从富人那里偷钱,然后把 ...
随机推荐
- hdu 4911 Inversion and poj2299 [树状数组+离散化]
题目 题意: 给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序对个数是多少. 给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序列所需要的最少步数 等于 整个序列的 ...
- 《mysql必知必会》学习_第19章_20180809_欢
第19章 插入数据 P132 insert into customers VALUES(NULL,'Pep E.Lapew','100 Main Street',,Los Angeles','CA', ...
- CNN 文本分类
谈到文本分类,就不得不谈谈CNN(Convolutional Neural Networks).这个经典的结构在文本分类中取得了不俗的结果,而运用在这里的卷积可以分为1d .2d甚至是3d的. 下面 ...
- CASUAL_NCT
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...
- Mac OS X 恢复 VMware Fusion 虚拟机中的 vmdk 文件
今天手贱把 VMware Fusion 虚拟机中的 Windows 10 搞挂了,原因是磁盘清理了下,然后重启就蓝屏了,Windows 10 自动修复.手动还原.手动重置系统,试过都不行,恢复系统是没 ...
- Visual Studio 2015 将json转换为实体类
最新写的一个接口需要接收json参数,然后序列化为实体类然后再进行后面的逻辑处理.因为json中键值对比较多,逐一去手写实体中的每个属性太麻烦,于是寻思是否有这样的工具可以将json转换为实体类. 经 ...
- [转]决策树在Kaldi中如何使用
转自:http://blog.csdn.net/chenhoujiangsir/article/details/51613144 说明:本文是kaldi主页相关内容的翻译(http://kaldi-a ...
- Java学习笔记48(DBUtils工具类一)
上一篇的例子可以明显看出,在增删改查的时候,很多的代码都是重复的, 那么,是否可以将增删改查封装成一个类,方便使用者 package demo; /* * 实现JDBC的工具类 * 定义方法,直接返回 ...
- 【Spark调优】数据本地化与参数调优
数据本地化对于Spark Job性能有着巨大的影响,如果数据以及要计算它的代码是在一起的,那么性能当然会非常高.但是,如果数据和计算它的代码是分开的,那么其中之一必须到另外一方的机器上.移动代码到其匹 ...
- python(33)——【re模块】
re模块(正则表达式) 就其本质而言,正则表达式是一种小型的.高度专业化的编程语言 在Python中(它内嵌在python中),并通过re模块来实现,正则表达式被编译成一系列的字节码,然后由C编写的匹 ...