POJ 1456 Supermarket [贪心+并查集]
来源:互联网 发布:编程助手 编辑:程序博客网 时间:2024/05/22 00:38
题目地址
贪心:优先选利益最大的商品,从deadline向前推直到都放不了再选下一个商品利益最大的
关键是判断哪一些日子不能再放了,最简单的方法就是一个数组+一个for循环判断
但更快的是用并查集,p=find(deadline) 当p>0,p就是可以放的日子 否则放不了;放好后更新parent[p]=p-1;向前一天递推
#include<iostream>#include<cstdio>#include<queue>#include<cmath> #include<map> #include<vector>#include<cctype>#include<cstring>#include<algorithm>using namespace std;int p[10000+5];struct product{int deadline,profit;product(int d,int p):deadline(d),profit(p){}bool operator < (const product& p) const {return profit<p.profit;}};int find(int x){return p[x]==x?x:p[x]=find(p[x]);}int main(){int T;priority_queue<product> Q;while(scanf("%d",&T)!=EOF){int pf,deadline,maxd=0;while(T--){cin>>pf>>deadline;maxd=max(maxd,deadline);Q.push(product(deadline,pf));}for(int i=0;i<=maxd;i++) p[i]=i;int ans=0,d;while(!Q.empty()){d=find(Q.top().deadline);if(d>0){p[d]=d-1;ans+=Q.top().profit;}Q.pop();}cout<<ans<<endl;}return 0;}
0 0
- poj 1456 Supermarket (贪心, 并查集)
- POJ 1456 Supermarket【贪心 + 并查集】
- poj 1456 - Supermarket(贪心+并查集)
- poj 1456 Supermarket (贪心+并查集)
- POJ 1456 Supermarket【贪心+并查集】
- POJ 1456 Supermarket [贪心+并查集]
- Supermarket poj 1456 (贪心,并查集)
- POJ-1456 Supermarket【贪心+并查集】
- 【经典贪心】poj 1456 Supermarket (贪心, 并查集)
- POJ 1456 Supermarket(贪心算法,可用并查集优化)
- Supermarket poj 1456 贪心+并查集优化
- POJ 1456 Supermarket(贪心 + 并查集 )
- POJ-1456 Supermarket(贪心,并查集优化)
- poj 1456 Supermarket 贪心 并查集优化
- POJ-1456 Supermarket 贪心 并查集优化
- POJ 1456 supermarket(贪心加并查集)
- Supermarket -- 贪心 + 并查集
- poj 1456 Supermarket(贪心+并查集,贪心+优先队列)
- 【操作系统】输入输出系统(中)-思维导图
- Java中对<对象> 和 <封装>的认识
- HDU 3746 Cyclic Nacklace (kmp求循环节)
- sk_buff封装和解封装网络数据包的过程详解
- tomcat - 报错 No such device; No faulty members identified.
- POJ 1456 Supermarket [贪心+并查集]
- JSP通过Servlet生成验证码
- 【操作系统】输入输出系统(上)-思维导图
- 【操作系统】虚拟存储器(下)-思维导图
- 专家系统介绍
- CoreData版本迁移(一)
- 【操作系统】虚拟存储器(上)-思维导图 :虚拟存储器概述、请求分页存储方式
- CPU acceleration status:HAXM must be updated(version 1.1.1<6.0.1)
- 【操作系统】对换、分页和分段方式-思维导图