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.
 
思路:以为是水题,直接读入累加,WA一次后发现有可能两个截止期长的都在前面卖,而不卖截止期短的,其实是贪心问题,价格排序,价格相同的截止日期大的在前,因为从大的开始,位置靠后的不能接纳截止日期小的,而位置靠前的可以两者都接受,要尽量多就要前者,可以用并查集模拟链表,加速判断某个日期是否可以卖
const int maxm = ;

struct Prod {
int p, d;
bool operator<(const Prod &a) const {
return p > a.p ||(p == a.p && d > a.d);
}
}Prods[maxm]; int fa[maxm]; void init() {
memset(fa, -, sizeof(fa));
} int Find(int x) {
if(fa[x] == -)
return x;
return fa[x] = Find(fa[x]);
} int main() {
int N, t1, t2;
while(scanf("%d", &N) != EOF) {
init();
for(int i = ; i < N; ++i) {
scanf("%d%d", &t1, &t2);
Prods[i] = {t1, t2};
}
sort(Prods, Prods+N);
int ans = ;
for(int i = ; i < N; ++i) {
int x = Find(Prods[i].d);
if(x > ) {
ans += Prods[i].p;
fa[x] = x - ;
}
}
printf("%d\n", ans);
}
return ;
}

Day5 - H - Supermarket POJ - 1456的更多相关文章

  1. (并查集 贪心思想)Supermarket -- POJ --1456

    链接: http://poj.org/problem?id=1456 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  2. Supermarket POJ - 1456

    A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...

  3. Supermarket POJ - 1456 贪心+并查集

    #include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...

  4. Supermarket POJ - 1456(贪心)

    题目大意:n个物品,每个物品有一定的保质期d和一定的利润p,一天只能出售一个物品,问最大利润是多少? 题解:这是一个贪心的题目,有两种做法. 1 首先排序,从大到小排,然后每个物品,按保质期从后往前找 ...

  5. POJ 1456 Supermarket 区间问题并查集||贪心

    F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  6. POJ 1456——Supermarket——————【贪心+并查集优化】

    Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  7. POJ 1456 - Supermarket - [贪心+小顶堆]

    题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...

  8. 【POJ 1456】 Supermarket

    [题目链接] http://poj.org/problem?id=1456 [算法] 贪心 + 堆 [代码] #include <algorithm> #include <bitse ...

  9. poj 1456 Supermarket - 并查集 - 贪心

    题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...

随机推荐

  1. 9 HTML DOM事件监听&版本兼容&元素(节点)增删改查

    事件监听: 语法:element.addEventListener(event, function, useCapture); event:事件的类型,触发什么事件,注意不需要on作为前缀,比如cli ...

  2. Linux系统在IT行业处于什么位置

    相信每一位程序员对于linux都不陌生,不管是新入行的小白,还是有着十几年编程经验的大佬,都知道Linux在IT行业中的位置吧! 我是一名Web   JAVA开发的小白,对于初入IT行业的小白来说,工 ...

  3. Spring Cloud Hystrix 请求熔断与服务降级

    在Java中,每一个HTTP请求都会开启一个新线程.而下游服务挂了或者网络不可达,通常线程会阻塞住,直到Timeout.你想想看,如果并发量多一点,这些阻塞的线程就会占用大量的资源,很有可能把自己本身 ...

  4. 使用java实现二叉查找树的插入,修改和删除方法

    目前使用的是根据key的hashcode来进行排序,并且没有考虑hash碰撞的问题 package com.zhou.tree; import java.util.Comparator; import ...

  5. MPAndroidChart柱子上的文字的颜色dataSet.setValueTextColors

    版本:MPAndroidChart v3.1.0 这是个很强大的图表,不同的版本对应的API会不一样. 需求描述: 用了柱状图,但要实现这样的功能,通过不同的门店来区分不同的柱子的颜色,并且柱子上文字 ...

  6. iOS Common Design Patterns:常用设计模式

    原文:http://www.jianshu.com/p/bf431fff235e 我们经常在编程中使用各种设计模式,在iOS中比较常见的设计模式有:单例模式.委托模式.观察者模式,当然实际上在Coco ...

  7. 从Uber司机只是合同工看,零工经济将受沉重打击?

    Uber自诞生以来,就始终处于漩涡之中--与当地的同类平台斗.创始人出现负面新闻.司机不断抗议--而就在近日"流血上市"后,Uber也没能迎来好时光.反而是股价不断下跌,市值疯狂蒸 ...

  8. 0-1背包问题(0-1 knapsack problem)

    0-1背包问题描述:一个正在抢劫商店的小偷发现了n个商品,第i个商品价值 vi 美元,重 wi 磅,vi 和 wi 都是整数.这个小偷希望拿走价值尽量高的商品,但他的背包最多能容纳 S 磅重的商品,S ...

  9. 新增6 n个骰子的点数

    /* * * 面试题43:n个骰子的点数 * 把n个骰子扔在地上,所有骰子朝上一面的点数之和为s. * 输入n,打印出s的所有可能的值出现的概率. * */ #include <iostream ...

  10. RHEL6配置CentOS yum源

    RHEL6配置CentOS yum源