二分求值(二分适合求答案在两个数之间的题目)

二分搜索,通过不断缩小解可能存在的范围,从而求得最优解的方法

题目链接:https://vjudge.net/contest/236677#problem/G

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,k, n代表有多少个地方,k代表有多少小时,下面n个数分别代表每个地方有多少人,每将一个人转移到另一个地方都需要一个小时。 题目要求你求出来在k的
时间内,人数最多的一个地方有多少人,要使这个数尽可能的小。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
typedef long long ll;
using namespace std;
ll n,m,ans;
ll a[100050];
ll judge(ll mid)
{
    ll coun=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]>mid)
            coun+=a[i]-mid;
    }
    if(coun>m)
        return 0;
    else
        return 1;
}
void Binary(ll l,ll r)
{
    ll mid=(l+r)/2;
    while(l<=r)
    {
        if(judge(mid))
        {
            ans=mid;
            r=mid-1;
        }
        else
            l=mid+1;
        mid=(l+r)/2;
    }
}
int main()
{
    ans=1e10;
    ll sum=0,mx=-1,ave;
    scanf("%I64d%I64d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%I64d",&a[i]);
        sum+=a[i];
        if(a[i]>mx)
            mx=a[i];
    }
    if(sum%n==0)
    {
        ave=sum/n;
    }
    else
        ave=sum/n+1;
    Binary(ave,mx);
    printf("%I64d\n",ans);
    return 0;
}

  

转载于:https://www.cnblogs.com/caijiaming/p/9279438.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值