POJ 1456--supermarket —————— "greedy + and search set optimization"

Source: Internet
Author: User
Tags time 0

SupermarketTime limit:2000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticePOJ 1456

Description

A supermarket have a set Prod of products on sale. It earns a profit px for each product X∈prod sold by a deadline DX that's measured as an integral number of time units St Arting 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 product Sell≤prod such that the selling of the all products 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 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 is listed in table 1. For instance, the schedule Sell={d,a} shows so the selling of product D starts at time 0 and ends at time 1 while the S Elling of product A starts at time 1 and ends at time 2. Each of the these products are sold by its deadline. Sell is the optimal schedule and its profit are 80.

Write a program This reads sets of products from an input text file and computes the profit of a optimal selling schedule For each set of 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 an end of file and is guaranteed correct.

Output

For each set of products, the program prints in the standard output the profit of a optimal selling schedule for the set. Each result was printed from the beginning of a separate line.

Sample Input

4-  2  1   2 1  2 1 3 2 8 2 5 50 10

Sample Output

80185

Hint

The sample input contains the 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 product is 185.   topic: give you n kinds of food, each food has pi and di respectively said that the food can sell Pi yuan, the shelf life is di days. After the shelf life can not be sold, that is not profitable. Each kind of food takes a day to sell out. Ask you how much money you can take in the best selling order.    problem-solving ideas: first consider greed. We sort the value of food from big to small. If the food is available for sale on the day of the shelf life (no other food is scheduled for sale on this day), then let it be sold on the day of the shelf life and try to make time for other foods. If it cannot be sold on the same day, we consider looking forward to the first time that no food is scheduled to be sold (with an array of tokens, if the food is scheduled for sale on the same day, then Mark True). This belongs to the violent search.     can be used and checked to optimize this brute force search, we can let the Father field of the Atlas show that the shelf food can be sold on SETS[X].PA this day. Let SETS[ROOTX].PA = rootx-1 each time. The current arrangement to rootx the shelf life is on sale at Rootx-1 this day.   
#include <stdio.h> #include <algorithm> #include <string.h> #include <iostream>using namespace std;const int MAXN = 1e5+200;struct product{int p,d;} products[maxn];struct set{int pa;} Sets[maxn];bool CMP (Product a,product b) {return A.P&GT;B.P;}    int Find (int x) {if (x = = SETS[X].PA) {return x;    } int tmp = SETS[X].PA; SETS[X].PA = Find (TMP); Path compression return SETS[X].PA;}    int main () {int n;        while (scanf ("%d", &n)!=eof) {for (int i = 1; I <= maxn-10;i++) {SETS[I].PA = i;        } for (int i = 1; I <= n; ++i) {scanf ("%d%d", &AMP;PRODUCTS[I].P,&AMP;PRODUCTS[I].D);        } sort (products+1,products+1+n,cmp);        int sum = 0;            for (int i = 1; I <= n;i++) {int rootx = Find (PRODUCTS[I].D);            if (rootx <= 0) {continue;              } SETS[ROOTX].PA = rootx-1;        sum + = PRODUCTS[I].P;    } printf ("%d\n", sum); } return 0;} 

  

POJ 1456--supermarket —————— "greedy + and search set optimization"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.