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++. ...
随机推荐
- 什么是Frozen Binary
对于Python来说,你可以将Python的字节码,PVM(也就是解析器),以及需要的相关类库,打包成一个package,这个package实际上是一个二进制可执行文件,这样,用户获取到这个packa ...
- 软工2017第四周作业结对编程——个人psp
29.22 --9.26本周例行报告 1.PSP(personal software process )个人软件过程. 类型 任务 预计时间 开始时间 结束时间 中断时间 ...
- Word Ladder Problem (DFS + BFS)
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- 老生常谈-从输入url到页面展示到底发生了什么
来自:咸鱼老弟 - 博客园 链接:http://www.cnblogs.com/xianyulaodi/p/6547807.html
- 第三章 持续集成jenkins工具使用之邮件配置
1 Email Extension Plugin插件安装 持续集成很重要的一环就是及时将构建结果通知到对应的责任人,如:构建失败了,至少需要下发通知给造成本次构建失败的开发人员,如果包含自动化测试 ...
- open-stf 安装篇(linux)
OpenSTF 百度MTC的远程真机调试 Testin的云真机 腾讯WeTest的云真机 阿里MQC的远程真机租用 什么是OpenSTF? OpenSTF是一个手机设备管理平台,可以对手机进行远 ...
- Perfmon - Windows 自带系统监控工具
一. 简述 可以用于监视CPU使用率.内存使用率.硬盘读写速度.网络速度等. Perfmon提供了图表化的系统性能实时监视器.性能日志和警报管理,系统的性能日志可定义为二进制文件.文本文件.SQLSE ...
- 在Centos中,大容量,且读写频繁的目录
1./根目录 2./usr目录 3./home目录 4./var目录 5./Swap目录 比较特殊,只要物理内存没使用完,就不会被启用 以上为鸟哥的linuxPDF中的学习心得
- sql sever误删数据库
在sql sever 2008 r2中,我想把一个数据库添加到DATA中,结果发现被占用,我就打算解除占用后再进行复制,本来应该先是让数据库脱离,再复制,结果,我自作聪明地右键数据库,选择了删除,结果 ...
- c++的一些编程技巧和细节
1.函数形参,如: CreateProcess( NULL, cmdbuf, NULL, ...