Link:
http://poj.org/problem?id=1456
Topic:
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 this is measured as a integral number of time units St Arting from the moment the sale begins. Each product takes precisely one of the being sold. A Selling schedule is A ordered subset of products Sell≤prod such this 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 are a schedule with a maximum profit.
For example, consider-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 this selling of product D starts at time 0 and ends in time 1 while the S Elling of product A starts at time 1 and ends in time 2. Each of the products are sold by its deadline. Sell is the optimal schedule and it profit is 80.
Write a program that reads sets of all products from a input text file and computes the profit of a optimal selling schedule For each set of the products.
Input
A set of products starts with an integer 0 <= n <= 10000, which are 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 Selli Ng deadline of the i-th product. White spaces can occur freely in input. Input data terminate with a end of file and are guaranteed correct.
Output
For each set of products, the program prints on the standard output the profit of a optimal selling for the set. The printed from the beginning of separate.
Sample Input
4 2 of 1 2 1 7, 1 2 1 3, 2 8 2 5 50 10
Sample Output
185
The main effect of the topic:
There are n products in the supermarket to sell, each product has a up to time DX (from the beginning of the sale), only before this time to sell and get the rate of run dy.
There are multiple products, all can have different selling order, each sell a product to occupy 1 units of time, ask the maximum can sell how much profit.
Ideas and Summary:
This is the same topic, but I look at how it looks like a greedy problem, so follow the greedy idea of AC.
Because each sell a product to occupy a time unit, so, we can be a unit a unit time to decide in turn, each time to sell which products, and to ensure that each unit time to sell products are the most profitable, so that the final result is the largest.
If the enumeration time from small to large, then more trouble, the better way is to start from the last time to enumerate forward, so long as the deadline is greater than this time period of the products are placed in the priority queue, where the most profitable is the time required. This method is implemented with sort + priority queue, and the complexity is n log n, which is 63ms on POJ.
But after all, and look at the collection of topics, and then think of and check the collection method to do. Thought for a long time, found it is really difficult to think. So secretly looked at the following use and check the set of the puzzle, found that can not understand ...
Time goes by 1.1, and suddenly I find that I can do it in another greedy way, first of all products in accordance with the profit from large to small sort, and then this put this on the day of the deadline to sell, and do a good job of marking, if the date of the end of the day already have other products occupied, then you can sell the time forward this product, Until you find a day to sell and mark it. According to this way of thinking submitted, AC, but the use of the mobile suit.
After using this method, and then recall the next and look up the code of the method, an instant epiphany, the so-called use and check set do, is actually the above method of optimization!
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
The key to using and looking at the collection is that we know that by the way above, suppose a product a takes up a date, then if the next time a product B and product A deadline is the same, but that date is occupied, so it is necessary to move forward for 1 days, then you can use and check the set to mark, After a takes up that date, point A's due date to the previous date, so that you can find out what time he takes. After using and searching the set optimization, the time is 47MS.
Code:
1. Greedy + priority queue
#include <cstdio> #include <algorithm> #include <queue> using namespace std;
const int N = 10005;
struct node{int px, DX;
friend bool operator< (const Node&a,const node&b) {return a.dx>b.dx;
}}arr[n];
Priority_queue<int,vector<int>,less<int> >q;
int n;
int main () {while (~SCANF ("%d", &n)) {int maxtime=0;
for (int i=0; i<n; ++i) {scanf ("%d%d", &arr[i].px, &ARR[I].DX);
if (arr[i].dx>maxtime) maxtime = ARR[I].DX;
Sort (arr,arr+n);
int ans = 0, pos=0;
while (!q.empty ()) Q.pop (); for (int t=maxtime; t>=1;--t) {while (pos<n&&arr[pos].dx>=t) {Q.push (arr[p
OS++].PX);
} if (!q.empty ()) {ans + = q.top ();
Q.pop ();
}
}printf ("%d\n", ans);
return 0; }
2. Another greedy way (No and search optimization)
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace St
D
const int N = 10005;
struct node{int px, DX;
friend bool operator< (const Node&a,const node&b) {return a.px>b.px;
}}arr[n];
int n;
int main () {while (~SCANF ("%d", &n)) {int maxtime=0;
for (int i=0; i<n; ++i) {scanf ("%d%d", &arr[i].px, &ARR[I].DX);
if (arr[i].dx>maxtime) maxtime = ARR[I].DX;
Sort (arr,arr+n);
BOOL Vis[n];
memset (Vis, 0, sizeof (VIS));
int ans = 0, pos=0;
for (int i=0; i<n; ++i) {if (!vis[arr[i].dx]) {ans = arr[i].px;
VIS[ARR[I].DX] = true; else{for (int j=arr[i].dx-1; j>=1;--j) if (!vis[j]) {ans = arr[
I].PX; Vis[j] = true;
Break
} printf ("%d\n", ans);
return 0; }
3. and search Set optimization