POJ 1456
阿新 • • 發佈:2021-06-12
由於這道題設定,一件商品的售出需要一天,因此倘若考慮用一個結構記錄當前打算售出的件數,這和結構中記錄元素數量是一致的。
這道題比較巧妙的是比貪心更近一步,使用最小堆進行優化
#include <iostream> #include <algorithm> #include <queue> #include <string> #include <vector> #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <stack> #include <map> #include <set> #include <deque> using namespace std; const int maxn= 1e4+5; struct Prod { int p, d; Prod (int _p= 0, int _d= 0) : p(_p), d(_d) {} bool operator < (const Prod &rhs) const { return p> rhs.p; } }pdt[maxn]; priority_queue<Prod> hp; bool cmp (const Prod &lhs, const Prod &rhs) { return lhs.d < rhs.d; } int main(int argc, char const *argv[]) { int n; while (~scanf("%d", &n)){ while (!hp.empty()){ hp.pop(); } for (int i= 0; i< n; ++i){ scanf("%d %d", &pdt[i].p, &pdt[i].d); } sort(pdt, pdt+n, cmp); for (int i= 0; i< n; ++i){ if (pdt[i].d > (int)(hp.size())){ hp.push(pdt[i]); } else if ((int)(hp.size())== pdt[i].d){ Prod can= hp.top(); if (can.p < pdt[i].p){ hp.pop(); hp.push(pdt[i]); } } } int ans= 0; while (!hp.empty()){ Prod cur= hp.top(); hp.pop(); ans+= cur.p; } printf("%d\n", ans); } return 0; }