zoukankan      html  css  js  c++  java
  • D

    题目链接:http://codeforces.com/problemset/problem/672/D

    D. Robin Hood
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.

    There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.

    After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.

    Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.

    Input

    The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) — the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.

    The second line contains n integers, the i-th of them is ci (1 ≤ ci ≤ 109) — initial wealth of the i-th person.

    Output

    Print a single line containing the difference between richest and poorest peoples wealth.

    Examples
    input
    Copy
    4 1
    1 1 4 2
    output
    Copy
    2
    input
    Copy
    3 1
    2 2 2
    output
    Copy
    0
    Note

    Lets look at how wealth changes through day in the first sample.

    1. [1, 1, 4, 2]
    2. [2, 1, 3, 2] or [1, 2, 3, 2]

    So the answer is 3 - 1 = 2

    In second sample wealth will remain the same for each person.

    题目大意:给你N和K , N个数 ,有K天,每天把最大的数减一,最小的数加一,问你第K天后,最大值和最小值的差值是多少

    思路:打比赛的时候一直想 的是二分这个差值,发现很多东西都控制不了, 结束后看题解发现是二分最小值和最大值

    注意一下边界问题就好了,看代码:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<stack>
    #include<map>
    using namespace std;
    typedef long long LL;
    const int INF=1e9+7;
    const int maxn=5e5+5;
    LL a[maxn];
    LL ave;
    LL N,K;
    bool judge(LL x)
    {
        LL sum=0;
        for(int i=1;i<=N;i++)
        {
            if(a[i]<x)
            {
                sum+=x-a[i];
            }
        }
        return sum<=K;
    }
    bool judge1(LL x)
    {
        LL sum=0;
        for(int i=1;i<=N;i++)
        {
            if(a[i]>x)
            {
                sum+=a[i]-x;
            }
        }
        return sum<=K;
    }
    int main()
    {
        scanf("%lld%lld",&N,&K);
        LL mi=INF,ma=-INF,ansl=0,ansr=0;
        LL sum=0;
        for(int i=1;i<=N;i++)
        {
            scanf("%lld",&a[i]);
            mi=min(mi,a[i]);
            ma=max(ma,a[i]);
            sum+=a[i];
        }
        ave=sum/N;
        LL l=mi,r=ave;
        while(l<=r)//最小值
        {
            LL mid=(l+r)>>1;
    //        cout<<mid<<endl;
            if(judge(mid))
            {
                ansl=mid;
                l=mid+1;
            }
            else r=mid-1;
        }
        l=ave;
        if(ave*N!=sum) l++;
        r=ma;
        while(l<=r)
        {
    
            LL mid=(l+r)>>1;
            if(judge1(mid))
            {
                ansr=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
    //    cout<<ansl<<" "<<ansr<<endl;
        printf("%lld
    ",ansr-ansl);
        return 0;
    }
    当初的梦想实现了吗,事到如今只好放弃吗~
  • 相关阅读:
    JAVA多线程知识点
    RabbitMQ和Springboot集成RabbitMQ知识点
    JAVA动态代理cglib或jdk
    [转]解决System.Data.SqlClient.SqlException (0x80131904): Timeout 时间已到的问题的一个方向
    [转]C#判断文档编码格式,并读取文档数据(防止出现乱码)
    create_linux命令写入到sh脚本并删除
    cmd cd切换到d盘
    sql 优化前后
    LISTAGG()WITHIN GROUP()
    使用shell递归遍历文件并打印所有文件名的绝对路径
  • 原文地址:https://www.cnblogs.com/caijiaming/p/11777821.html
Copyright © 2011-2022 走看看