博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1456 Supermarket
阅读量:5090 次
发布时间:2019-06-13

本文共 5089 字,大约阅读时间需要 16 分钟。

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #define max(a, b) ((a) > (b) ? (a) : (b)) 7 8 const int INF = 0x3f3f3f3f; 9 const int MAXN = 10000 + 10;10 const int MAXP = 10000 + 10;11 12 inline void read(int &x)13 {14 x = 0;char ch = getchar(), c = ch;15 while(ch < '0' || ch > '9')c = ch, ch = getchar();16 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();17 if(c == '-') x = -x; 18 }19 20 int fa[MAXP], rank[MAXN], value[MAXN], t[MAXN], cnt[MAXN], n, ans, ma;21 22 int find(int x)23 {24 return fa[x] == x ? x : fa[x] = find(fa[x]);25 }26 27 bool cmp(int a, int b)28 {29 return value[a] > value[b];30 }31 32 int main()33 {34 while(scanf("%d", &n) != EOF)35 {36 ans = ma = 0;37 for(register int i = 1;i <= n;++ i)38 read(value[i]), read(t[i]), cnt[i] = i, ma = max(ma, t[i]);39 for(register int i = 1;i <= ma;++ i)40 fa[i] = i;41 std::sort(cnt + 1, cnt + 1 + n, cmp);42 register int p;43 for(register int i = 1;i <= n;++ i)44 {45 p = cnt[i];46 int tmp = find(t[p]);47 if(tmp != 0) ans += value[p], fa[tmp] = tmp - 1;48 }49 printf("%d\n", ans);50 }51 return 0;52 }
POJ1456 并查集做法

 

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #define max(a, b) ((a) > (b) ? (a) : (b)) 8 9 const int INF = 0x3f3f3f3f;10 const int MAXN = 10000 + 10;11 const int MAXP = 10000 + 10;12 13 inline void read(int &x)14 {15 x = 0;char ch = getchar(), c = ch;16 while(ch < '0' || ch > '9')c = ch, ch = getchar();17 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();18 if(c == '-') x = -x; 19 }20 21 int value[MAXN], t[MAXN], cnt[MAXN], n, ans, ma;22 23 bool cmp(int a, int b)24 {25 return t[a] > t[b];26 } 27 28 struct cmpp29 {30 bool operator()(int a, int b)31 {32 return value[a] < value[b];33 } 34 };35 36 std::priority_queue
, cmpp> q;37 38 int main()39 {40 while(scanf("%d", &n) != EOF)41 {42 while(q.size())q.pop();43 ans = ma = 0;44 for(register int i = 1;i <= n;++ i)45 read(value[i]), read(t[i]), cnt[i] = i;46 std::sort(cnt + 1, cnt + 1 + n, cmp);47 int p = 1;ma = t[cnt[1]];48 for(register int i = ma;i >= 1;-- i)49 {50 while(t[cnt[p]] == i)q.push(cnt[p]), ++p;51 if(q.size())ans += value[q.top()],q.pop();52 }53 printf("%d\n", ans);54 } 55 return 0;56 }
POJ1456 优先队列做法

 

Supermarket
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13387   Accepted: 6019

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 17  20 1   2 1   10 3  100 2   8 2   5 20  50 10

Sample Output

80185

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

 
【题解】
  贪心题目,都是优先买价值高的,需要保证时间合法。不难发现过期晚的可以在这个时间之前任意时刻买,所以时间一般降序处理。
  做法一:可以降序枚举时间,把对应时间刚好马上就要过期但没过期的加入优先队列中,每个时间从优先队列取出价值最高的即可。正确性显然
       做法二:用并查集维护某个时间向前追溯到的第一个没有买东西的时间。然后按价值降序枚举物品,把他的时间p所代表的父亲t合并到t-1上,如果t是0代表不能选。正确性亦显然。
       标程见上房
 
 
 

转载于:https://www.cnblogs.com/huibixiaoxing/p/7326037.html

你可能感兴趣的文章
人生哲学
查看>>
JAVA调用.NET的WEBSERVICE
查看>>
Selenium+Python浏览器调用:Firefox
查看>>
nohup 详解
查看>>
树莓派实现摄像头监控(使用motion和mjpg-streamer)
查看>>
《转》推荐系统经典论文文献及业界应用
查看>>
webpack的像素转vw单位的loader插件
查看>>
javascript高级程序设计一书----关于创建和对象继承的总结
查看>>
媒体电话
查看>>
Web开发者欣喜若狂的40个UI设计工具和资源
查看>>
整数拼数 C语言版
查看>>
WPF 绘制曲线图
查看>>
/proc 目录详细说明
查看>>
你的灯还亮着吗阅读笔记之二
查看>>
Hive(2)-Hive的安装,使用Mysql替换derby,以及一丢丢基本的HQL
查看>>
在固定宽度 下计算出实际的行高
查看>>
hdu 1873 看病要排队
查看>>
多线程学习笔记
查看>>
《城市化》(顾朝林)-重要术语
查看>>
GridPanel 带头和锁定列共存
查看>>