【hdu 6438】Buy and Resell

【链接】 我是链接,点我呀:)
【题意】

有一个物品的价格在1..n这些位置各不相同。 你初始有无限的钱。 问你从1走到n. 你每次可以选择买入一个或者卖出一个该种物品(或啥都不做) 问你最后的最大利润。

【题解】

定义一个数据类型为pair temp的优先队列q。 其中second的值为1或者2 **如果为1** 那么说明这个订单代表的是一个"已经配对"的订单。 即代表有序对(x,y)且x

【代码】

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

const int N = 1e5;

int n;
int a[N+10];
long long ans,num;
priority_queue <pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;

int main()
{
    //freopen("D:\cpp_program\rush0\rush.txt","r",stdin);
    ios::sync_with_stdio(0),cin.tie(0);
    int T;
    cin >> T;
    while (T--){
        ans = 0;num = 0;
        cin >> n;
        for(int i = 1;i <= n;i++)  cin >> a[i];
        while (!q.empty()) q.pop();
        for (int i = 1;i <= n;i++){
            if (!q.empty() && q.top().first<a[i]){
                pair<int,int> temp = q.top();q.pop();
                ans+=a[i]-temp.first;
                if (temp.second==1){
                    q.push(make_pair(temp.first,2));
                }else{
                    num+=2;
                }
                q.push(make_pair(a[i],1));
            }else q.push(make_pair(a[i],2));
        }
        cout<<ans<<' '<<num<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/9649309.html