poj 1456 Supermarket - 并查集 - 贪心

题目传送门

  传送点I

  传送点II

题目大意

  有$n$个商品可以销售。每个商品销售会获得一个利润,但也有一个时间限制。每个商品需要1天的时间销售,一天也只能销售一件商品。问最大获利。

  考虑将出售每个物品尽量外后安排。这样当一个商品不能安排的时候看能不能替换掉它能够出售的时间中盈利最小的商品。

  因此可以将物品排序,这样只用考虑能否让每个物品出售。

  为了找到第一个空闲时间,又因为已经安排的时间不会改变,所以用并查集将已经安排了出售的时间段缩起来。

Code

 1 /**
 2  * poj
 3  * Problem#1456
 4  * Accepted
 5  * Time: 47ms
 6  * Memory: 768k
 7  */
 8 #include <algorithm>
 9 #include <iostream>
10 #include <cstdlib>
11 #include <cstring>
12 #include <cstdio>
13 #include <queue>
14 using namespace std;
15 typedef bool boolean;
16
17 typedef class Product {
18     public:
19         int profit;
20         int deadline;
21
22         Product() {    }
23
24         boolean operator < (Product b) const {
25             return profit > b.profit;
26         }
27 }Product;
28
29 const int N = 1e4 + 5;
30
31 int n, res;
32 int pre[N];
33 Product *ps;
34 boolean vis[N];
35
36 inline boolean init() {
37     if (scanf("%d", &n) == EOF)
38         return false;
39     res = 0;
40     ps = new Product[(n + 1)];
41     for (int i = 1; i <= n; i++)
42         scanf("%d%d", &ps[i].profit, &ps[i].deadline);
43     return true;
44 }
45
46 int findPre(int p) {
47     if (!p)    return 0;
48     if (!vis[p]) {
49         vis[p] = true;
50         return p;
51     }
52     return pre[p] = findPre(pre[p]);
53 }
54
55 inline void solve() {
56     sort(ps + 1, ps + n + 1);
57     memset(vis, false, sizeof(vis));
58     for (int i = 1; i <= 10000; i++)
59         pre[i] = i - 1;
60     for (int i = 1; i <= n; i++) {
61         int p = findPre(ps[i].deadline);
62         if (p)
63             res += ps[i].profit;
64     }
65     printf("%d\n", res);
66     delete[] ps;
67 }
68
69 int main() {
70     while (init())
71         solve();
72     return 0;
73 }

原文地址:https://www.cnblogs.com/yyf0309/p/9420556.html

时间: 2024-07-29 17:38:50

poj 1456 Supermarket - 并查集 - 贪心的相关文章

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

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

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 2492 (简单并查集) A Bug&#39;s Life

题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋.然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋 这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的时候要简单一些 sex数组保存的是与父节点的性别关系,如果与父节点是同性,则为0,否则是1 每次路径压缩的同时要更新sex[a] = (sex[a] + sex[temp]) % 2; 还有就是如果x 和 y 不在一个集合,两棵树进行合并的时候,考虑x px y py 四者之间的关系,有 paren

HDU 1051 并查集+贪心

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11694    Accepted Submission(s): 4837 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

zoj 3659 Conquer a New Region 并查集+贪心

点击打开链接题目链接 Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history rolling forward, our king conquered a new region in a distant continent. There are N towns (numbered from 1 to N) in this region connected by s

POJ - Colored Sticks - 并查集+字典树

这道题主要还是要判断是不是欧拉图 说白了就是能不能这幅图能不能用一笔画下来,那么就可以知道了,如果是一个环状的,说明奇数度就不存在,否则就只能用两个奇数度(起点终点)//我的理解这是 只需要用字典树将单词变为对应的一个数字,然后并查集操作就可以,需要维护一个度变量 #include<stdio.h> #include<string.h> int du[500010],p[500010]; int tot=1; struct tree { tree *next[30]; int id

poj 2513 欧拉回路+并查集判断是否联通+Trie树

http://poj.org/problem?id=2513 最初看到 第一感觉---map  一看250000的数据量 果断放弃 然后记得以前看过,trie代替map,尤其当数据量特别大的时候 学到了: 1.Trie代替map的思想,可以在单词结尾的tree[i][tk]  这个i作为字符串对应的int值 ,当然这个int值也可以用于建立并查集 2.接上,通过并查集判断,所有的点在同一个集合图就是联通的,否则不联通,注意tree[i][tk]>0 表示是单词结尾, x=Find(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