• 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;
    }
  • 相关阅读:
    【配置属性】—Entity Framework 对应表字段的类型的设定配置方法
    EntityFrame Work 6 Code First 配置字段为varchar 类型
    Echarts xAxis boundaryGap
    JavaScript Array和string的转换
    SQL server :主键和外键
    SQL server :“增删改查” 之 “改”
    SQL server :“增删改查” 之 “删”
    SQL server :“增删改查” 之 “增”
    Oracle不能连接故障排除【TNS-12541:TNS:无监听程序】
    LNMP平台部署及应用
  • 原文地址:https://www.cnblogs.com/noback-go/p/11157529.html
Copyright © 2020-2023  润新知