题目大意:超市有n件物品,每件物品有响应的利润p和保质期d,每天只能卖出一件物品,必须得在改保质期前卖出才能赚到利润,问如何卖这些物品才能使利润达到最大
解题思路:按保质期的降序排序,然后从最大的保质期开始枚举,设置一个优先队列,利润大的排前面
因为时间t是从大到小开始枚举的,所以如果保质期的d >= t的话,就可以将其放在队列中了,因为接下来时间的减小的,所以t<=d这个条件是恒满足的,所以只需从优先队列中挑出p最大的就可以了
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 10010
struct Pro{
int p, d;
bool operator < (const Pro t) const {
return p < t.p;
}
}P[maxn];
int n;
int cmp(const Pro a, const Pro b) {
return a.d > b.d;
}
int main() {
while(scanf("%d",&n) == 1) {
int MAX_t = 0;
for(int i = 0; i < n; i++) {
scanf("%d%d",&P[i].p, &P[i].d);
MAX_t = max(MAX_t, P[i].d);
}
sort(P,P+n,cmp);
priority_queue<Pro> q;
int ans = 0, j = 0;
for(int i = MAX_t; i > 0; i--) {
while(j < n && P[j].d >= i)
q.push(P[j++]);
if(!q.empty()) {
ans += q.top().p;
q.pop();
}
}
printf("%d\n",ans);
}
return 0;
}