POJ 1456 Supermarket 贪心+优先队列

12 篇文章 0 订阅
3 篇文章 0 订阅

可以从截止日期最大的一天开始循环,优先队列来维护队列中的利益最大值,把截止日期等于当天的加入到队列中,每个日期在队列中有成员时都要选其中的最大值即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<set>
#include<bitset>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#define INF 0x3f3f3f3f
#define inf 2*0x3f3f3f3f
#define llinf 1000000000000000000
#define pi acos(-1.0)
#define mod 1000000007
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define lb(x) (x&-x)
#define gi(x) scanf("%d",&x)
#define gi2(x,y) scanf("%d%d",&x,&y)
#define gll(x) scanf("%lld",&x)
#define gll2(x,y) scanf("%lld%lld",&x,&y)
#define gc(x) scanf("%c",&x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
/***********************************************/
int n;
struct node
{
    int profit,en;
    bool operator<(const node &a)const
    {
        return profit<a.profit;
    }
}product[10005];
bool cmp(node x,node y)
{
    return x.en>y.en;
}
int main()
{
    while(gi(n)!=EOF)
    {
        rep(i,0,n)gi2(product[i].profit,product[i].en);
        sort(product,product+n,cmp);
        priority_queue<node>que;
        int sum=0,pos=0,up=product[0].en;
        while(up>0)
        {
            while(pos<n&&product[pos].en==up)que.push(product[pos++]);
            if(!que.empty())
            {
                sum+=que.top().profit;que.pop();
            }
            up--;
        }
        cout<<sum<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值