zoukankan      html  css  js  c++  java
  • 二分求值(二分适合求答案在两个数之间的题目)

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

    题目链接: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
    ",ans);
        return 0;
    }
    

      

    当初的梦想实现了吗,事到如今只好放弃吗~
  • 相关阅读:
    Django【二】自定义web框架
    Django【一】web框架的本质
    Django【零】HTTP协议
    【前端】bootstrap引入
    【前端】jquery基础学习
    socket模块粘包现象理解以及解决思路
    面向对象编程学习笔记
    socket模块
    CPU缓存体系对Go程序的影响
    测试Go代码 单元测试
  • 原文地址:https://www.cnblogs.com/caijiaming/p/9279438.html
Copyright © 2011-2022 走看看