hdu 6438 贪心+优先队列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fanbaobao829/article/details/82225599
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<int,int> f;
int main()
{
    int t,n,cnt,tp;
    ll ans;
    scanf("%d",&t);
    while(t--&&scanf("%d",&n)!=EOF)
    {
        priority_queue<int,vector<int>,greater<int> >que;
        cnt=0;
        ans=0;
        f.clear();
        for(int i=0;i<n;i++)
        {
            scanf("%d",&tp);
            if(que.size()&&que.top()<tp)
            {
                ans+=tp-que.top();
                if(f[que.top()])
                {
                    f[que.top()]--;
                    cnt--;
                }
                f[tp]++;
                cnt++;
                que.pop();
                que.push(tp);
            }
            que.push(tp);
        }
        printf("%lld %d\n",ans,cnt<<1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/fanbaobao829/article/details/82225599