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

Input
5 4
3 4 1 4 9
Output
5
Input
2 1000000000
1000000000 4
Output
500000002
Input
5 3
2 2 2 2 1
Output
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[100005];
    while(cin>>n>>hour)
    {
        ll sum = 0,aver,Max = -1;
        for(int i=0; i<n; ++i)
        {
            scanf("%lld",&num[i]);
            sum += num[i]; //求出所有数的和 
            if(num[i] > Max) //求所有数的最大值 
                Max = num[i];
        }
        if(sum%n == 0) //求平均值 
            aver = sum/n;
        else aver = sum/n +1;
        
        ll flag = 0,low = aver,high = Max,mid,all; //初始化二分的左边界为平均值,右边界为最大值 
        while(low <= high) //二分 
        {
            all = 0;
            mid = (high + low) / 2; //求二分的中间值 
            for(int i=0; i<n; ++i) 
            {
                if(num[i] > mid) //求出所有比中间值大的数与中间值的差的和 
                    all += num[i] - mid;
            }
            if(all == hour) //如果这个和刚好与拥有的时间相等,则不需要查找了,这就是答案 
            {
                flag = 1;
                break;
            }
            else if(all > hour) low = mid + 1; //如果和比中间值大,则答案在比中间值大的范围内 
            else high = mid - 1; //否则,答案在比中间值小的范围内 
        }
        if(flag)
            cout<<mid<<endl;
        else
            cout<<low<<endl;
    }
    return 0;
}
 
  

转载于:https://www.cnblogs.com/tuyang1129/p/9279426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值