POJ - 1456 贪心 堆常用操作 注意细节

题意:给定n个商品的deadline和profit,求每天卖一件的情况下的最大获利

显然是一道贪心

按deadline从小到大排序好,动态维护小根(profit)堆的大小<=当前deadline的天数,往里面符合条件的尽可能塞更优解

注意有n为0的情况

还有脑抽导致判断条件缺斤少两,下次不要这样了

/*H E A D*/
struct Node{
    ll p,d,id;
}a[maxn];

bool cmp(Node a,Node b){
    if(a.d!=b.d)return a.d<b.d;
    return a.p>b.p;
}
ll n;
int main(){
    while(cin>>n){
        ll day=0;
        priority_queue<ll,vector<ll>,greater<ll> > que;
        while(!que.empty())que.pop();
        rep(i,1,n){
            a[i].p=read();
            a[i].d=read();
            a[i].id=i;
            day=max(day,a[i].d);
        }
        if(n==0){
            println(0);
            continue;
        }
        sort(a+1,a+1+n,cmp);
        ll ans=0;
        int now=1;
        rep(i,1,day){
            while(a[now].d<i) now++;
            while(a[now].d==i&&que.size()<i&&now<=n){//equal!!!!
                que.push(a[now].p);now++;
            }
            while(a[now].d==i&&que.size()==i&&now<=n&&que.top()<a[now].p){
                que.pop();
                que.push(a[now].p);
                now++;
            }
        }
        while(!que.empty()){
            ans+=que.top();
            que.pop();
        }
        println(ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/caturra/p/8439998.html

时间: 2024-07-29 16:42:27

POJ - 1456 贪心 堆常用操作 注意细节的相关文章

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

poj 1456(贪心、并查集)

Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15416   Accepted: 6941 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 in

POJ 1456 贪心 并查集

看一下中文版的题目就好,英文题目太晦涩了. 有两种方法可以解题 一种是贪心+优先队列 另一种是贪心+并查集 优先队列  需要说的都在代码注释里 #include<cstdio> #include<queue> #include<algorithm> using namespace std; struct s{ int day, val; }arr[100005]; bool cmp(s a, s b){ if(a.day != b.day) return a.day &

POJ 1456 (贪心+并查集) Supermarket

有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少 这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润 朴素的做法是: 按照每件商品的利润从大到小排序,有一个busy数组记录那天是否有东西卖出.对于每件商品,从它的截止日期开始遍历,如果那天有东西卖就看看前一天是否有卖东西,直到有一天没有东西卖或者前面的天数都有卖的. 1 //#define LOCAL 2 #include <iostream>

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 (贪心+并查集)

# 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——————【贪心+并查集优化】

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

[ACM] POJ 2442 Sequence (堆的性质)

Sequence Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7011   Accepted: 2262 Description Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It's

1 数据结构(13)_二叉树的概念及常用操作实现

1. 树到二叉树的转换 思考:通用树结构的实现太过复杂(树中每个结点都可以有任意多的孩子,具有多种形态),工程中很少会用到如此复杂的树是否可以简化呢?思路:减少树结点中孩子的数量.但这样树是否还能通用呢? 1.1.树的两种表示法 双亲孩子表示法:孩子兄弟表示法:孩子兄弟表示法的特点:1.能够表示任意的树形结构2.每个结点包含一个数据成员和两个指针成员3.孩子结点指针和兄弟结点指针构成"树杈" 2.2.二叉树 二叉树是由n(n>=0)个节点组成的有限集合,该集合或者为空,或者是由一