Supermarket
| Time limit:2000 ms |
|
Memory limit:65536 K |
| Total submissions:7501 |
|
Accepted:3148 |
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 limit ≤ prod such that the selling of each product X records, according to the ordering of orders, completes before the deadline DX or just when DX expires. the profit of the selling schedule is profit (FIT) = Σ 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) = (), (Pb, DB) = ), (PC, DC) = (20, 2), and (PD, DD) = (30, 1 ). the possible selling schedules are listed in Table 1. for instance, the schedule tables = {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. operation 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 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
Southeastern Europe 2003 |
In fact, we use greed and query sets as tools to speed up the process.
The general idea is to buy and sell n items. Each item has an end time. You can buy it before the end time, and you can only buy one item per unit time. Maximum profit.
If the purchase does not conflict, you can buy all of them. If there is a conflict, you need to choose between them. Obviously, in the case of conflicts, we choose higher prices and better prices, so we can use greedy
Algorithm . First, sort the items in the order of price to the end. If you buy one, you will mark it at the time point. You can purchase the item as long as there is no conflict. How can we quickly find the first time point without conflict? I personally feel that this problem is well solved by using and querying the set. The function of checking the set here is similar to the linked list pointer. The compression process is the process of deleting nodes. So as to find the non-conflicting point in O (1) time.
/* Poj 1456 greedy processing. Sort by profit P in ascending order. Select the last non-conflict point during processing. Implement the functions of the linked list using the parallel query set to quickly locate non-conflicting points */ # Include <Stdio. h> # Include < String . H> # Include <Iostream> # Include <Algorithm> Using Namespace STD; Const Int Maxn = 10010 ; Int F [maxn]; Struct Node { Int P, D;} node [maxn]; Bool CMP (node A, Node B) // Sort by P in ascending order. D does not matter { Return A. P> B. P ;} Int Find ( Int X ){ If (F [x] =-1 ) Return X; Return F [x] = Find (F [x]);} Int Main (){ Int N; While (Scanf ( " % D " , & N) = 1 ) {Memset (F, -1 , Sizeof (F )); For ( Int I = 0 ; I <n; I ++ ) Scanf ( " % D " , & Node [I]. P ,& Node [I]. d); sort (node, node + N, CMP ); Int Ans = 0 ; For ( Int I = 0 ; I <n; I ++ ){ Int T = Find (node [I]. D ); If (T> 0 ) {Ans + = Node [I]. P; F [T] = T- 1 ;} Printf ( " % D \ n " , ANS );} Return 0 ;}