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;
    }
  • 相关阅读:
    8.【原创】使用Java8中的Stream特性筛选数据
    27.【转载】如何避免回表查询,什么是索引覆盖
    17.【转载】广东省2020学位英语考试报考须知
    Java 正则?:?=?!的理解
    集合的优化操作
    ArrayList中remove方法和set(null)的区别
    POI 导入、导出Excel
    JS 跳转到新页面并用post传参
    win 10 如何关闭自动更新
    JSON定义及应用
  • 原文地址:https://www.cnblogs.com/noback-go/p/11157529.html
Copyright © 2011-2022 走看看