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.
Multiple groups of test data, each group of N items, given the value and period of sale, require that the product be sold before the period, only one product can be sold at a time, to find the maximum value.
Idea: Get drunk when you see the question... Naked greedy, but O (N ^ 2) is also drunk... Later, I realized that I could use heap for optimization, but I wouldn't write it !!! I am not XXY. So I used and optimized the query set... Amazing !!!
We regard continuous occupied intervals as a set (subtree), and its root node is the first unoccupied interval on the left of this interval. Sort first, and then judge whether find (B [I]) is greater than 0 each time. If it is greater than 0, it indicates that there is still unoccupied space on the left, and it will be occupied, then Merge (Rool (B [I]) and Rool (B [I])-1. Similarly, we stipulate that only the left subtree can be merged to the right subtree.
After understanding the method, the code is very good...
Code:
# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> using namespace STD; int Fa [10001] = {0}; struct use {int va, ti;} A [10001]; int Rool (int x) {If (Fa [x]! = X) Fa [x] = Rool (Fa [x]); Return Fa [X];} int my_comp (const use & X, const use & Y) {If (X. va> Y. va) return 1; else {If (X. va = y. va & X. ti <Y. ti) return 1; else return 0 ;}int main () {int N, I, j, R1, maxn; long ans; while (scanf ("% d ", & n) = 1) {ans = 0; maxn = 0; for (I = 1; I <= N; ++ I) {scanf ("% d", & A [I]. va, & A [I]. ti); if (a [I]. ti> maxn) maxn = A [I]. ti;} for (I = 1; I <= maxn; ++ I) Fa [I] = I; sort (a + 1, A + n + 1, my_comp ); for (I = 1; I <= N; ++ I) {R1 = Rool (A [I]. ti); If (r1> 0) {Fa [R1] = Rool (r1-1); ans = ans + A [I]. va ;}} printf ("% LLD \ n", ANS );}}
(Additional: codevs1052 is similar)
Poj1456 supermarket (! Easy)