poj 1456 supermarket

题目大意:

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

思路:

优先队列

按照截止日期由大到小排序,从最大截止日期开始,一天天向前推直到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);
    }
}

时间: 2024-11-06 07:06:13

poj 1456 supermarket的相关文章

poj 1456 Supermarket (贪心+并查集)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int fa[10010]; struct node { int p; int d; }; struct node a[10010]; bool cmp(node a1,node a2)//利润从大到小 { return a1.p>a2.p; } int find(int x) { if(fa[x]

POJ 1456 Supermarket 区间问题并查集||贪心

F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1456 Appoint description:  System Crawler  (2015-11-30) Description A supermarket has a set Prod of products on sale. It earns a p

POJ 1456——Supermarket——————【贪心+并查集优化】

Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1456 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx

poj 1456 Supermarket - 并查集 - 贪心

题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品尽量外后安排.这样当一个商品不能安排的时候看能不能替换掉它能够出售的时间中盈利最小的商品. 因此可以将物品排序,这样只用考虑能否让每个物品出售. 为了找到第一个空闲时间,又因为已经安排的时间不会改变,所以用并查集将已经安排了出售的时间段缩起来. Code 1 /** 2 * poj 3 * Prob

POJ 1456 Supermarket(贪心+并查集)

Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the sale begins. Each product takes precise

nyoj 208 + poj 1456 Supermarket (贪心)

Supermarket 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the

贪心/poj 1456 Supermarket

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 struct node 6 { 7 int value; 8 int ddl; 9 }; 10 node a[10010]; 11 bool day[10010]; 12 int n,ans; 13 bool cmp(node x,node y) 14 { 15 return x.value>y

poj 1456 Supermarket(并查集维护区间)

 题意:有一些货物,每个货物有价值和卖出的截至日期,每天可以卖一个货物,问能卖出的最大价值是多少. 思路:算法不难想到,按价值降序排列,对于每一件货物,从deadline那天开始考虑,如果哪天空闲那么将货物在该天卖出. 如果直接暴力来做,复杂度为o(n*n),显然不行.可以用并查集来维护当前ddl之前空闲的最近的一天,如果父节点为0说明deadline之前没有空闲的时间,那么该货物就无法卖出. #include<cstdio> #include<cstring> #includ

Supermarket poj 1456 贪心+并查集优化

Language: Default Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9512   Accepted: 4096 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is