Poj1456supermarket (greedy + priority queue)

Source: Internet
Author: User
Tags printf time 0
Supermarket
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9103 Accepted: 3891

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 cont Inues 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 is guaranteed correct.

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

Sample Input

4,  2, 1, 2,   1

7,  1   2  , 1 3, 2 8 2 5, 50, 10

Sample Output

185

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 test instructions: There are n products in the supermarket to sell, each product has a time-up DX (from the start of the sale), only in this time before the sale and get the rate of run dy. There are multiple products, all can have a different order of sale, each selling a product to occupy 1 units of time, ask the most can sell how much profit. Problem solving: The product by the deadline from the big to the small row, and then from the largest period of time to move forward day by day, if the current day of the term product is not completely sold, put to the previous period, and the last deadline of the product a walk to sell, the big profit on the first sell. No selling is put on the day before to sell, in this way from the large term to the small term pushes, because the product of the small term cannot go to sell in the large term time, while the product of the large term can be sold in small terms.

#include <stdio.h> #include <iostream> #include <queue> #include <algorithm> using namespace std
; #define N 10005 typedef struct NNN {int p,d;}
Node
    typedef struct NN {int p;
    friend bool operator< (struct nn a,struct nn b)//Priority queue value from large to small take {return a.p<b.p;
}}int;
Node Ans[n];
    int CMP (node A,node b)//deadline from large to small row {return a.d>b.d;} int solve (int n,int l) {int maxp=0,tl,d,i,maxd;
    priority_queue<int>q;
    INT pp;
    i=0; For (MAXD=ANS[0].D, maxd>0;maxd--) {for (;i<n&&maxd<=ans[i].d; i++) {pp. P=ANS[I].P;
        Q.push (PP);
        } tl=0;
            while (!q.empty () &&tl<l) {pp=q.top (); Q.pop (); MAXP+=PP.P;
        tl++;
}} return MAXP;

    } int main () {int n,l,i;
        while (scanf ("%d", &n) >0) {for (i=0;i<n;i++) scanf ("%d%d", &AMP;ANS[I].P,&AMP;ANS[I].D);
       if (n==0) {printf ("0\n"); continue;
        } sort (ans,ans+n,cmp);
    printf ("%d\n", Solve (n,1));
 }
}


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.