码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1456 supermarket

时间:2017-07-22 18:18:38      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:str   ++   分享   输出   stream   gif   can   pre   false   

题目大意:

有很多物品要卖,每种物品都有自己的价值和截止日期,每个物品都必须在截止日期前卖出去,求最后获得的最大价值和是多少

思路:

优先队列

按照截止日期由大到小排序,从最大截止日期开始,一天天向前推直到1

如果某天是一个东西的截止日期,就把这件东西的价值加到优先队列里,每天都从优先队列里卖一个东西,ans+=q.top(),然后pop

最后输出ans

技术分享
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
int now,n,ans,maxn;
struct st
{
    int v,t;
    bool operator < (const st &a) const
    {
        if(t>a.t) return true;
        return false;
    }
}a[10101];
int main()
{
    while(cin>>n)
    {
        ans=0;
        priority_queue <int> q;
        for(int i=0;i<n;i++) 
        {
            scanf("%d%d",&a[i].v,&a[i].t);
        }
        sort(a,a+n);
        int k=0;
        int tm=a[0].t;
        for(int i=tm;i>=1;i--)
        {
            while(k<n&&a[k].t>=i) {q.push(a[k].v);k++;}
            if(!q.empty()){ans+=q.top();q.pop();}
        }
        printf("%d\n",ans);
    }
}
View Code

 

poj 1456 supermarket

标签:str   ++   分享   输出   stream   gif   can   pre   false   

原文地址:http://www.cnblogs.com/yyc-jack-0920/p/7221914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!