zoukankan      html  css  js  c++  java
  • F:Honk's pool (The Preliminary Contest for ICPC Asia Shenyang 2019)

    As we all know, Honk has nn pools, numbered as 11 ~ nn . There is a_iai liters water in the ii-th pool. Every day, Honk will perform the following operations in sequence.

    1. Find the pool with the most water (If there are more than one, choose one at random) and take one liter of water.

    2. Find the pool with the least water (If there are more than one, choose one at random) and pour one liter of water into the pool.

    3. Go home and rest (Waiting for the next day).

    Please calculate the difference between the amount of water in the pool with the most water and the amount of water in the pool with the least water after the kk days.

    Input

    The input consists of multiple test cases. The input is terminated by the end of file.The number of data sets will not exceed 40

    The first line of each test case contains two integers nn and kk, which indicate the number of pools and the number of days to operate the pool.

    The second line of each test case contains nn integers, and the ii-th number represent a_iai indicating the initial amount of water in the ii-th pool.

    1 le n le 5000001n500000, 1 le k le 10^91k109, 1 le a_i le 10^91ai109.

    Output

    For each test case, print one line containing the answer described above.

    样例输入1

    4 100
    1 1 10 10
    

    样例输出1

    1
    

    样例输入2

    4 3
    2 2 2 2
    

    样例输出2

     

    不得不承认队友的天赋比我高多了,思维是真的强!
    然后,一定要去魔改各种方法!!!



    由于昨天的数据比较弱,所以这个代码虽然能过,但是依然存在着很大的bug,所以呢,只推荐理解。

    用二分来做的话,嗯~,就是先找二分达到稳定状态(就是达到平均值average
    )然后判断所需的步数cnt,
    如果cnt<=k(给定步数),判断稳定状态有几个值
      一个的话为
    average,两个的话average和average+1一个的话为0,反之为1
    如果cnt>k,嗯~,二分k步时的最大值最小值,二分在k步时,average左边的最小值,以及average右边的最大值,做差就可以了
      这一步类似于二分题“月度开销”的过程



    #include <iostream>
    #include <cmath>
    #include <map>
    #include <queue>
    #include <algorithm>
    
    using namespace std;
    
    long long a[500010];
    
    int main()
    {
        int n,k;
        std::ios::sync_with_stdio(false);
        priority_queue<long long,vector<long long>,less<long long> >q;
        priority_queue<long long,vector<long long>,greater<long long> >p;
        while(cin>>n>>k)
        {
            while(!q.empty())
                q.pop();
            while(!p.empty())
                p.pop();
            int flag=0;
            long long sum=0,arv,num=0;
            for(int i=0;i<n;i++)
            {
                cin>>a[i];
                q.push(a[i]);
                p.push(a[i]);
                sum+=a[i];
            }
            if(sum%n)
                flag=1;
            arv=sum/n;
            sort(a,a+n);
            for(int i=0;i<n;i++)
            {
                if(a[i]==arv||(flag&&a[i]==arv+1))
                    continue;
                num+=abs(arv-a[i]);
            }
            long long x,y;
            if(k>=num/2)
            {
                if(flag)
                    cout<<1<<endl;
                else
                    cout<<0<<endl;
            }
            else
            {
                while(k--)
                {
                    x=q.top();
                    y=p.top();
                    q.pop();p.pop();
                    x--;y++;
                    q.push(x);
                    p.push(y);
                }
                cout<<q.top()-p.top()<<endl;
            }
        }
        return 0;
    }

     

    所遇皆星河
  • 相关阅读:
    poj1014 hdu1059 Dividing 多重背包
    浏览器开发工具
    Linux dirname、basename 指令
    linux ar命令
    gcc创建和使用静态库、动态库
    -DDEBUG编译标记
    sigaction函数解析
    Signal ()函数详细介绍 Linux函数
    Linux makefile 教程 非常详细,且易懂
    初始用户组,有效用户组,初始用户,有效用户
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11520300.html
Copyright © 2011-2022 走看看