The Preliminary Contest for ICPC Asia Shenyang 2019 F. Honk's pool

题目:https://nanti.jisuanke.com/t/41406

思路:特判+优先队列+优化

#include
using namespace std;
int main()
{
    int n,k;
    int a;
    while(~scanf("%d%d",&n,&k))
    {
        priority_queue<int> q1;
        priority_queue<int,vector<int>,greater<int> > q2;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a);
            q1.push(a);
            q2.push(a);
        }
        if(n==2) 
        {
            if((q1.top()-q2.top())/2<k)
            {
                if((q1.top()-q2.top())&1) printf("1\n");
                else printf("0\n");
            }
            else printf("%d\n",q1.top()-q2.top()-2*k);
            continue;
        }
        for(int i=1;i<=k;)
        {
            if(q1.top()==q2.top()) break;
            int tq1=q1.top();
            q1.pop();
            int res1=tq1-q1.top();
            int tq2=q2.top();
            q2.pop();
            int res2=q2.top()-tq2;
            if(!res1||!res2) q1.push(tq1-1),q1.push(tq2+1),q2.push(tq1-1),q2.push(tq2+1),i++;
            else if(res1>res2) 
            {
                if(i+res2>k) res2=k-i+1;
                q1.push(tq1-res2),q1.push(tq2+res2),q2.push(tq1-res2),q2.push(tq2+res2),i+=res2;    
            }
            else 
            {
                if(i+res1>k) res1=k-i+1;
                q1.push(tq1-res1),q1.push(tq2+res1),q2.push(tq1-res1),q2.push(tq2+res1),i+=res1;
            }
        }
        printf("%d\n",abs(q1.top()-q2.top()));
    }
    return 0;
}

你可能感兴趣的:(The Preliminary Contest for ICPC Asia Shenyang 2019 F. Honk's pool)