AcWing - 145 - Supermarket = greedy

https://www.acwing.com/problem/content/147/

There are n commodities, commodity price and expiration date before they can sell before the expiration time, you can only sell a day. Seeking to maximize profits.

If ordering directly to the expiration time will then greed WA. In fact first of all items sorted by expiration date, the price of goods into the top of a small heap inside, detected a product's expiration time <the current size of the heap, indicating that not enough time now to sell all the merchandise, put a minimum price the discarded. Why is this right? After each pile of things have been adjusted either sell finished yet sufficient, then the rest of the goods between fill extra time. Otherwise (the current time point) just good enough, each item can be found in a time before he expired sell.

If the new merchandise added to the list of lowest price, direct lost it. Otherwise, I swap out a product, this is not a new product added to the list, then it is swapped out to accommodate the new position certainly enough of that commodity.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

pair<int, int> p[10005];
priority_queue<int, vector<int>, greater<int> > pq;

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    int n;
    while(~scanf("%d", &n)) {
        for(int i = 1; i <= n; ++i) {
            int x, y;
            scanf("%d%d", &x, &y);
            p[i] = {y, x};
        }
        sort(p + 1, p + 1 + n);
        for(int i = 1; i <= n; ++i) {
            pq.push(p[i].second);
            while(p[i].first < pq.size())
                pq.pop();
        }
        ll sum = 0;
        while(!pq.empty()) {
            sum += pq.top();
            pq.pop();
        }
        printf("%lld\n", sum);
    }
}

Guess you like

Origin www.cnblogs.com/Inko/p/11515413.html