Gym - 100989G (二分法)
There are K hours left before Agent Mahone leaves Amman! Hammouri doesn't like how things are going in the mission and he doesn't want to fail again. Some places have too many students covering them, while other places have only few students.
Whenever Hammouri commands a student to change his place, it takes the student exactly one hour to reach the new place. Hammouri waits until he is sure that the student has arrived to the new place before he issues a new command. Therefore, only one student can change his place in each hour.
Hammouri wants to command the students to change their places such that after K hours, the maximum number of students covering the same place is minimized.
Given the number of students covering each place, your task is to find the maximum number of students covering the same place after K hours, assuming that Hammouri correctly minimizes this number.
Input
The first line of input contains two integers M (2 ≤ M ≤ 105) and K (1 ≤ K ≤ 109), where M is the number of places and K is the number of hours left before Agent Mahone leaves Amman.
The second line contains M non-negative integers, each represents the number of students covering one of the places. Each place is covered by at most 109 students.
Output
Print the maximum number of students covering a place after K hours, assuming that Hammouri minimized this number as much as possible in the last K hours.
Examples
5 4
3 4 1 4 9
5
2 1000000000
1000000000 4
500000002
5 3
2 2 2 2 1
2 题意:给了你n个数和一个时间,一个单位时间可以修改一次数据,修改的方法是将n个数中的某一个加1,而另一个减1(相当于把某个数的1送给另一个数),问你在规定时间内如何修改这n个数,可以使得这n个数中的最大值是所有修改方法中最小的,输出最小值。 思路:大佬的思路,不知道大佬的脑子是什么做的,这都能想出来(应该是我太菜)!!!首先要明确一点,这n个数不管如何修改,最后的最大值一定大于等于这n个数的平均值,而且一定小于等于没修改前的最大值,所以最后的答案一定在这两者之间。所以我们就可以用二分法对这两者之间的所有数进行二分查找。查找的每一次都需要判断是否符合要求:遍历所有的数,记录下这些数中比当前二分的中间值mid大的数,他们与mid的差的和是多少,如果和比你所拥有的时间多,说明不符合要求,你无法将所有的数都修改到中间值这么小,你的答案要比这个中间值大,所以二分的左边界low变成mid+1;否则右边界变成mid-1,一直到最后high<low结束查找。具体看代码:
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
ll n,hour,num[];
while(cin>>n>>hour)
{
ll sum = ,aver,Max = -;
for(int i=; i<n; ++i)
{
scanf("%lld",&num[i]);
sum += num[i]; //求出所有数的和
if(num[i] > Max) //求所有数的最大值
Max = num[i];
}
if(sum%n == ) //求平均值
aver = sum/n;
else aver = sum/n +; ll flag = ,low = aver,high = Max,mid,all; //初始化二分的左边界为平均值,右边界为最大值
while(low <= high) //二分
{
all = ;
mid = (high + low) / ; //求二分的中间值
for(int i=; i<n; ++i)
{
if(num[i] > mid) //求出所有比中间值大的数与中间值的差的和
all += num[i] - mid;
}
if(all == hour) //如果这个和刚好与拥有的时间相等,则不需要查找了,这就是答案
{
flag = ;
break;
}
else if(all > hour) low = mid + ; //如果和比中间值大,则答案在比中间值大的范围内
else high = mid - ; //否则,答案在比中间值小的范围内
}
if(flag)
cout<<mid<<endl;
else
cout<<low<<endl;
}
return ;
}
Gym - 100989G (二分法)的更多相关文章
- Gym - 100989G 二分
链接:ECJTU 2018 Summer Training 1 - Virtual Judge https://vjudge.net/contest/236677#problem/G 谷歌翻译: 距 ...
- Gym 101246H ``North-East''(LIS)
http://codeforces.com/gym/101246/problem/H 题意: 给出n个点的坐标,现在有一个乐队,他可以从任一点出发,但是只能往右上方走(包括右方和上方),要经过尽量多的 ...
- Gym - 100283F F. Bakkar In The Army —— 二分
题目链接:http://codeforces.com/gym/100283/problem/F F. Bakkar In The Army time limit per test 2 seconds ...
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- C语言两种查找方式(分块查找,二分法)
二分法(必须要保证数据是有序排列的): 分块查找(数据有如下特点:块间有序,块内无序):
- poj3122-Pie(二分法+贪心思想)
一,题意: 有f+1个人(包括自己),n块披萨pie,给你每块pie的半径,要你公平的把尽可能多的pie分给每一个人 而且每个人得到的pie来自一个pie,不能拼凑,多余的边角丢掉.二,思路: 1,输 ...
随机推荐
- ceph---luminous版的安装
前言 ceph luminous版本新增加了很多有意思的功能,这个也是一个长期支持版本,所以这些新功能的特性还是很值得期待的,从底层的存储改造,消息方式的改变,以及一些之前未实现的功能的完成,都让ce ...
- 好记性不如烂笔头-Mysql查找如何判断字段是否包含某个字符串
好记性不如烂笔头-Mysql查找如何判断字段是否包含某个字符串 利用mysql 字符串函数 find_in_set(); SELECT * FROM users WHERE find_in_set(' ...
- vue-cli 自定义过滤器的使用
vue-cli 自定义过滤器的使用 vue2.0将内置过滤器去除,所以过滤器需要自己编写. Vue.js 允许你自定义过滤器,可被用作一些常见的文本格式化.过滤器可以用在两个地方:mustache 插 ...
- System.Drawing.Text.TextRenderingHint 的几种效果
; i < ; i++) { g5.TextRenderingHint = (System.Drawing.Text.TextRenderingHint)i; string txt; ; txt ...
- halcon 如何把一个region截取出来保存为图像
read_image(Image,'monkey') gen_circle(region,200,200,150) reduce_domain(Image,region,Mask) crop_doma ...
- Django xadmin的使用 (二)
上一篇中我们基本完成了xadmin的配置,但是要进行正式使用还需要更多细致的配置. 1.页面显示中文 settings.py中配置(这个和django自带的admin配置一样) # LANGUAGE_ ...
- 【转】SVN 与 GIT 详细对比
git和svn的详细对比 近期就[版本管理工具是否进行切换SVN->Git]的问题进行了讨论,于是对svn和Git进行了相关研究,进而梳理出Git的特点(优.缺点),最后将Git与SVN进行 ...
- C#模板的效率问题
1,有拆装箱的情景时,可使用模板方式避免拆装箱,这时候使用模板比不使用效率要高很多. 2,无拆装箱的操作时,全部是值传递,使用模板会比使用基本类型慢一半
- java 蓝桥杯算法提高 _2最大最小公倍数
解题思路: 1. n是奇数,那就最大的三个数相乘2. n是偶数,得分两种情况了, ①如果n不是3的倍数,那就s=n*(n-1)*(n-3)---n与n-2同为偶数,故排除一个n-2: ②n是3的倍数, ...
- Oracle 下基于 DBMS_RESOURCE_MANAGER 包估算数据库存储 IO 性能
:first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; m ...