zoukankan      html  css  js  c++  java
  • Gym-100989G(二分)

    G. Mission in Amman (B)

    time limit per test
    1.0 s
    memory limit per test
    256 MB
    input
    standard input
    output
    standard output

    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 Khours, 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

    题目链接:http://codeforces.com/gym/100989/problem/G

    题目大意:M个城市,K次移动。每次移动可以移动一个人,通过移动使得M个城市中人数最多的城市人数最少。

    解题思路:通过二分查找,判断从人数的平均值到给出城市人数的最大值可以通过K次操作到达的最小值。

    代码如下:

    #include<stdio.h>
    
    long long ans;
    long long n,m;
    long long a[110000];
    long long ave,sum=0;
    
    bool judge(long long mid)
    {
        long long cnt=0;
        for(long long i=1;i<=n;i++)
        {
            if(a[i]>mid) cnt+=a[i]-mid;
        }
        if(cnt<=m) return 1;
        else return 0;
    }
    
    void solve(long long l,long long r)
    {
        long long mid=(l+r)/2;
        while(l<=r)
        {
            if(judge(mid))
            {
                ans=mid;
                r=mid-1;
            }
            else
            {
                l=mid+1;
            }
            mid=(l+r)/2;
        }
        printf("%lld
    ",ans);
    }
    
    int main()
    {
        long long maxx=-1;
        scanf("%lld%lld",&n,&m);
        for(long long i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
            if(a[i]>maxx) maxx=a[i];
        }
        ave=sum/n;
        if(sum%n) ave++;
        solve(ave,maxx);
        return 0;
    }
  • 相关阅读:
    基于API函数的串口通信编程——理解(转)
    如何设置超时参数(串行通讯)
    注册热键——RegisterHotKey
    如何制作OEM包 (转帖)
    CreateThread函数创建线程
    WinCe6.0 播放语音问题
    C语言编写图形程序// http://lcrystal.spaces.live.com/blog/cns!13dfc6235bbfb72c!128.entry
    关于串口编程的读书笔记
    c++中内存管理
    MFC读取TXT文件,写入编辑框
  • 原文地址:https://www.cnblogs.com/noback-go/p/11157529.html
Copyright © 2011-2022 走看看