题目链接:http://poj.org/problem?id=1456

Supermarket
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13736   Accepted: 6206

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 precisely one unit of time for being sold. A selling schedule is an ordered subset of products Sell ≤ Prod such that the selling of each product x∈Sell, according to the ordering of Sell, completes before the deadline dx or just when dx expires. The profit of the selling schedule is Profit(Sell)=Σx∈Sellpx. An optimal selling schedule is a schedule with a maximum profit. 
For example, consider the products Prod={a,b,c,d} with (pa,da)=(50,2), (pb,db)=(10,1), (pc,dc)=(20,2), and (pd,dd)=(30,1). The possible selling schedules are listed in table 1. For instance, the schedule Sell={d,a} shows that the selling of product d starts at time 0 and ends at time 1, while the selling of product a starts at time 1 and ends at time 2. Each of these products is sold by its deadline. Sell is the optimal schedule and its profit is 80. 

Write a program that reads sets of products from an input text file and computes the profit of an optimal selling schedule for each set of products. 

Input

A set of products starts with an integer 0 <= n <= 10000, which is the number of products in the set, and continues with n pairs pi di of integers, 1 <= pi <= 10000 and 1 <= di <= 10000, that designate the profit and the selling deadline of the i-th product. White spaces can occur freely in input. Input data terminate with an end of file and are guaranteed correct.

Output

For each set of products, the program prints on the standard output the profit of an optimal selling schedule for the set. Each result is printed from the beginning of a separate line.

Sample Input

4  50 2  10 1   20 2   30 1

7  20 1   2 1   10 3  100 2   8 2
5 20 50 10

Sample Output

80
185

Hint

The sample input contains two product sets. The first set encodes the products from table 1. The second set is for 7 products. The profit of an optimal schedule for these products is 185.

Source

 
 
 
题解:
1.由于每种商品只需销售一天(要是天数不确定又该怎么办?),且一天只能销售一种商品。所以,在某一天,销售的商品的获利越大越好。
2.对于一种商品,应该尽可能把它推到过期前才销售,这样就可以腾出时间卖其他商品,使获利最大化。
3.贪心:根据商品的获利进行降序排序,用数组used[]标记某一天是否已经确定要卖商品。对每一件商品进行操作:对于该商品,首选的出售时间是deadline,如果deadline已经被其他商品占用了(该商品的获利更大),那么就往前找,找到第一个能用的天,然后将该天标记。
4.对于怎样找到第一个能用的天,有两种方法。第一种方法,也是最直接最暴力的:往前枚举,在此不细述。第二种方法是利用:并查集的路径压缩。
 
并查集的路径压缩: 
1.初始化每一天为可用,即fa[x] = -1。
2.在处理的过程中,如果某一天day被这个商品用了,那么就将fa[day]设为day-1表明:后面的商品如果想用这一天,是行不通的,只能用前面的天,那究竟是哪一天呢?day这一天,作为一个“好心人”,已经为商品指明了方向:去找fa[day],如果fa[day]也用了,那就去找fa[fa[day]]……直到找到能用的天。
3.利用并查集的路径压缩,在搜索路径上的点,都直接指向了根节点(根节点即为可用的一天),从而减少了搜索长度。
 
 
枚举:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; struct node
{
int p, d;
bool operator<(const node &x)const{
return p>x.p;
}
}a[MAXN];
int used[MAXN], n; int main()
{
while(scanf("%d", &n)!=EOF)
{
memset(used, , sizeof(used));
for(int i = ; i<=n; i++)
scanf("%d%d", &a[i].p, &a[i].d);
sort(a+, a++n); int ans = ;
for(int i = ; i<=n; i++)
{
for(int j = a[i].d; j>=; j--)
if(!used[j])
{
used[j] = ;
ans += a[i].p;
break;
}
} printf("%d\n", ans);
} }
 
路径压缩优化:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; struct node
{
int p, d;
bool operator<(const node &x)const{
return p>x.p;
}
}a[MAXN];
int used[MAXN], fa[MAXN], n; int find(int x) { return (fa[x]==-)?x:fa[x]=find(fa[x]); } int main()
{
while(scanf("%d", &n)!=EOF)
{
memset(used, , sizeof(used));
memset(fa, -, sizeof(fa));
for(int i = ; i<=n; i++)
scanf("%d%d", &a[i].p, &a[i].d);
sort(a+, a++n); int ans = ;
for(int i = ; i<=n; i++)
{
int j = find(a[i].d);
if(j>)
{
fa[j] = j-;
ans += a[i].p;
}
} printf("%d\n", ans);
}
}

POJ1456 Supermarket —— 贪心 + 路径压缩优化的更多相关文章

  1. poj1456 Supermarket 贪心+并查集

    题目链接:http://poj.org/problem?id=1456 题意:有n个物品(0 <= n <= 10000) ,每个物品有一个价格pi和一个保质期di (1 <= pi ...

  2. 并查集路径压缩优化 UnionFind PathCompression(C++)

    /* * UnionFind.h * 有两种实现方式,QuickFind和QuickUnion * QuickFind: * 查找O(1) * 合并O(n) * QuickUnion:(建议使用) * ...

  3. POJ1456 Supermarket 贪心

    贪心策略:一定先卖价值最大的,然后考虑卖当前的物品,卖的日期越靠后,越优,可以为以后的物品提供机会 #include <stdio.h> #include <string.h> ...

  4. POJ-1456 Supermarket 贪心问题 有时间限制的最小化惩罚问题

    题目链接:https://cn.vjudge.net/problem/POJ-1456 此题与HDU-1789完全是一道题 题意 有N件商品,分别给出商品的价值和销售的最后期限,只要在最后日期之前销售 ...

  5. 关于并查集的路径压缩(Path Compress)优化

    之前在CSDN看到一篇很受欢迎的讲解并查集的博文,其中自然用到了路径压缩: int pre[1000]; int find(int x){ int root = x; while(pre[root]! ...

  6. HDU 6326.Problem H. Monster Hunter-贪心(优先队列)+流水线排序+路径压缩、节点合并(并查集) (2018 Multi-University Training Contest 3 1008)

    6326.Problem H. Monster Hunter 题意就是打怪兽,给定一棵 n 个点的树,除 1 外每个点有一只怪兽,打败它需要先消耗 ai点 HP,再恢复 bi点 HP.求从 1 号点出 ...

  7. Google Pagespeed,自动压缩优化JS/CSS/Image

    Google Pagespeed,自动压缩优化JS/CSS/Image 浏览: 发布日期:// 分类:技术分享 关键字: Nginx Appache Pagespeed 自动压缩优化JS/CSS/Im ...

  8. Apache配置压缩优化时报错——undefined symbol: inflateEnd

    Apache配置压缩优化时报错——undefined symbol: inflateEnd 环境:CentOS 6.4 软件版本:httpd-2.4.6 apr-1.4.8 apr-util-1.5. ...

  9. 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction

    http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...

随机推荐

  1. Codeforces 696E ...Wait for it...(树链剖分)

    题目链接  ...Wait for it... 考虑树链剖分. 对于树上的每个点开一个set,记录当前该节点上所有的girls. 每个节点初始的权值为set中的最小值. 询问的时候每次在路径上寻找最小 ...

  2. ubuntu系统克隆

    使用clonezilla,原文地址:http://www.linuxidc.com/Linux/2014-09/107117.htm 类似的一篇:http://storysky.blog.51cto. ...

  3. python装饰器的深度探究

    1.讲装饰器一般讲到这种代码就可以了,但这篇博客会介绍更多: def deco(func): def wrapper(): print("start") func() #调用函数 ...

  4. 【ZJOI2017】树状数组

    题目描述 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的 OI 比赛经历.那是一道基础的树状数组题. 给出一个长度为 $n$ 的数组 $A$,初始值都为 $0$,接下来 ...

  5. 把Execl表格中的数据获取出来保存到数据库中

    比如我们遇到一些需要把execl表格中的数据保存到数据库中,一条一条保存效率底下而且容易出错,数据量少还好,一旦遇到数据量大的时候就会累死个人啊,下面我们就来把execl表格中数据保存到对应的数据库中 ...

  6. Android传统View动画与Property动画基础及比较

    前言:关于动画方面的知识也整理一段时间了,如题,这篇文章简单的介绍了View和Property动画的概念,如何在项目中创建资源文件,以及如何在代码中使用它们,本次整理动画的重点放在了Property动 ...

  7. 【Exception】查看异常出现在具体的文件名/类名/方法名/具体行号

    今天在处理异常日志保存过程中,想要获取到异常抛出在具体在那个文件,哪个类下的哪个方法中的具体第几行,所以具体实现如下 try{ Integer adminID = Integer.parseInt(a ...

  8. 椭圆人头跟踪bmp图像序列 BMP Image Sequences for Elliptical Head Tracking

    BMP Image Sequences for Elliptical Head Tracking The BMP image sequences used in the head tracking d ...

  9. 开源软件许可认证:open softwae license

    OSIA认证的开放源代码软件的软件许可证有如下21种: 1.The GNU General Public License (GPL) 2.The GNU Library or "Lesser ...

  10. Java中char转为16进制

    Java中char转为16进制 char a = '0'; String hexStr = Integer.toHexString(a); System.out.println(hexStr);