您的位置:首页 > 其它

UVALive - 2757 Supermarket 贪心+优先队列

2015-03-18 16:34 567 查看
题目大意:超市有n件物品,每件物品有响应的利润p和保质期d,每天只能卖出一件物品,必须得在改保质期前卖出才能赚到利润,问如何卖这些物品才能使利润达到最大

解题思路:按保质期的降序排序,然后从最大的保质期开始枚举,设置一个优先队列,利润大的排前面

因为时间t是从大到小开始枚举的,所以如果保质期的d >= t的话,就可以将其放在队列中了,因为接下来时间的减小的,所以t<=d这个条件是恒满足的,所以只需从优先队列中挑出p最大的就可以了

[code]#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: