"POJ 1456" supermarket

Source: Internet
Author: User
Tags time 0

Supermarket
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9560 Accepted: 4116

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 are 185.

Source

Southeastern Europe 2003

Application of greedy + check set ~


First of all greedy, sort by profit, put each in the last position he can put.


So how do you quickly determine the last position he can place?


Use and look up the set ~


If I was used on this day, Fa=getfather (i) then F[fa]=f[fa-1],f[i] indicates that f[i]+1 to I have been used.


If fa=0 indicates that the item cannot be added.


#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include < Cstdlib> #define M 10000+5using namespace Std;int n,f[m];struct data{int p,d;} A[m];void read (int &tmp) {Tmp=0;char ch=getchar (); int fu=1;for (;ch< ' 0 ' | | Ch> ' 9 '; Ch=getchar ()) if (ch== '-') fu=-1;for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) tmp=tmp*10+ch-' 0 '; tmp *=fu;} BOOL CMP (data A,data b) {if (A.P==B.P) return A.d<b.d;return A.P>B.P;} int getfather (int x) {return x==f[x]?x:f[x]=getfather (f[x]);} int main () {        while (scanf ("%d", &n)!=eof) {int mad=0;for (int i=1;i<=n;i++) read (A[I].P), read (A[I].D), mad= Max (MAD,A[I].D); sort (a+1,a+1+n,cmp); int ans=0;for (int i=0;i<=mad;i++) f[i]=i;for (int i=1;i<=n;i++) {int fa= Getfather (A[I].D); if (FA) {ans+=a[i].p;f[fa]=f[fa-1];}} printf ("%d\n", ans);} return 0;}


Sentiment:

1.WA because F[fa]=f[fa-1], first written f[a[i].d]=f[a[i].d-1]


"POJ 1456" supermarket

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.