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,输 ...
 
随机推荐
- Python压缩及解压文件
			
Zip压缩 #-*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import zipfile #加载模块 # 压缩 z = zipf ...
 - python实现cifar10数据集的可视化
			
在学习tensorflow的mnist和cifar实例的时候,官方文档给出的讲解都是一张张图片,直观清晰,当我们看到程序下载下来的数据的时候,宝宝都惊呆了,都是二进制文件,这些二进制文件还不小,用文本 ...
 - Django学习---CSRF
			
CSRF xss攻击:假设我们网站的评论里面允许用户写js的时候,每个人就会看到页面会执行这个js代码,有的是alert,不停的跳出弹框.这个还不算严重的,关键是如果js代码运行的结果不显示在页面上, ...
 - Python 小知识点(10)--异常结构记录
			
try: print("try中") except KeyError as e: # 异常时,执行该块 print("异常中") else: # 主代码块(tr ...
 - django admin 中实现word文档下载
			
为了实现此功能,需要用到的知识点: (1)django admin 自定义字段: 参考:http://www.cnblogs.com/wumingxiaoyao/p/6928297.html ...
 - [iOS]UIScrollView嵌套UITableView,超出屏幕的cell点击不了问题
			
最初我是用UIScrollView嵌套了一个UIView,然后UIView里面嵌套UITableView,这样cell 就会超出屏幕那一部分点击不了. 解决方法如下,UITableView拖出来,作为 ...
 - mysql数据库怎么安装到自己的电脑上?
			
安装mysql1.安装过程中输⼊入Y确认继续安装 2.设置密码1.第⼀一次设登陆密码 2.第⼆二次验证密码 安装完成
 - jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
			
jsp出现getOutputStream() has already been called for this response异常的原因和解决方法 在tomcat5下jsp中出现此错误一般都是在js ...
 - 压缩包法安装mysql
			
之前一直安装mysql,我一直是用压缩包安装的.如果之前安装过Mysql,必须要删除注册文件,才能把Mysql彻底删除了. 先在官网下载mysql的版本.下载package版本,即.zip版本的.下载 ...
 - css的优先级和权重问题 以及!important优先级
			
一,前言: 刚加的css怎么没有渲染出来?浏览器中查看,发现是被其他的css给覆盖了,相信我们都曾遇到过这样的问题.那么浏览器是如何选择css标签的渲染顺序的呢?换句话说,css选择器的优先级是怎么规 ...