HDU 6438 Buy and Resell

高卖低买,可以交易多次

维护一个优先队列,贪心

相当于每天卖出

用当前元素减优先队列最小得到收益

用0/卖出,1/买入标志是否真实进行了交易,记录次数

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll A[500005];
#define P pair<ll,ll>
priority_queue<P,vector<P>,greater<P> > q;
int main()
{
    int T,N;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        ll ans=0,t,tt=0;
        while(!q.empty())q.pop();
        for(int i=0;i<N;i++){
            scanf("%lld",&A[i]);
            q.push(P(A[i],0));
            q.push(P(A[i],1));
            t=A[i]-q.top().first;
            if(q.top().second==1)tt+=2;
            q.pop();
            ans+=t;

        }
        cout<<ans<<' '<<tt<<'\n';
    }
}

 

posted @ 2019-08-17 09:27  liulex  阅读(113)  评论(0编辑  收藏  举报