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;
    }
  • 相关阅读:
    再谈应用程序单例运行(只能开启一个进程)
    wpf 无边框窗口 最大化,缩放问题修复
    AutoClose=false的情况下自动关闭下拉框(ToolStripHost)
    C#调用其他语言(如PB)的com组件
    再写Javascript闭包
    Vue 数据双向绑定的误区
    后台数据缓存的一点心得
    C# 使用 Newtonsoft.Json 对DataTable转换中文乱码问题的解决
    第二章 Background & Borders 之 Multiple borders
    第二章 Background & Borders 之 Transparent Border
  • 原文地址:https://www.cnblogs.com/noback-go/p/11157529.html
Copyright © 2011-2022 走看看