HDU-6438-Buy and Resell

思路参考自https://www.cnblogs.com/zbh2047/p/9736378.html

  • 贪心
    Accepted 6438 234MS 2300K 1054 B G++
    #include "bits/stdc++.h"
    using namespace std;
    typedef long long LL;
    const int MAXN = 1e5 + 5;
    struct Node {
        int v;
        // op表示在此城市的操作,true为卖出,false为不操作,因为无需把买入的城市加入优先队列所以这里不考虑买入。 
        bool op;
        friend bool operator < (Node n, Node m) {
            // 先按城市物价排序,在价格相同的情况下为保证购买次数最少,优先选择op为true的城市。 
            if (n.v != m.v) {
                return n.v > m.v;
            }
            return !n.op;
        }
    };
    priority_queue<Node> qu;
    int main() {
        int t, k, cnt;
        Node n, m;
        LL res;
        scanf("%d", &t);
        while (t--) {
            res = cnt = 0;
            scanf("%d", &k);
            for (int i = 0; i < k; i++) {
                scanf("%d", &n.v);
                n.op = false;
                if (!qu.empty()) {
                    Node m = qu.top();
                    if (m.v < n.v) {
                        n.op = true;
                        qu.pop();
                        if (m.op) {
                            m.op = false;
                            qu.push(m);
                            res += n.v - m.v;
                        } else {
                            res += n.v - m.v;
                            cnt += 2;
                        }
                    }
                }
                qu.push(n);
            }
            printf("%lld %d\n", res, cnt);
            while (!qu.empty()) {
                qu.pop();
            }
        }
        return 0;
    }

     

posted @ 2019-02-22 16:56  Jathon-cnblogs  阅读(144)  评论(0编辑  收藏  举报