Buy and Resell hdu-6438 贪心 优先队列

题目链接:Problem - 6438

题面:

 

题意:你按顺序去n个城市,每个城市商品买入和卖出的价格一样,你可以用一天时间买入或者卖出,问最多可以获取多少钱,已经最少的天数

思路:在每个位置处贪心寻找之前的可以购买的物品中价格最低的,用map维护一下已经卖出过的价格,当现在买入的价格之前被卖出过,就可以减少购买次数 

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
map<int, int> mp;
int main(){
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        mp.clear();
        int n;
        cin >> n;
        priority_queue<int, vector<int>, greater<int>> q;
        ll sum = 0;
        int num = 0;
        for(int i = 0; i < n; i++){
            int x;
            cin >> x;
            if(!q.empty() && (x > q.top())){
                if(mp[q.top()] == 0){
                    num += 2;
                }else{
                    mp[q.top()]--;
                }
                mp[x]++;
                sum += x - q.top();
                q.pop();
                q.push(x);
            }
            q.push(x);
        }
        cout << sum << " " << num << endl;
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值