Nyoj 1107 Highest Reward (greedy + priority queue)

Source: Internet
Author: User

maximum reward time limit: ms | Memory limit:65535 KB Difficulty: 3
Describe

Excuse me: Which is the best excavator technology? AC has told you!

Give you N (n<=3*10^4) tasks, each task has a deadline to finish T (1=<t<=10^9) and to complete the task of the Reward V (1=<v<=10^9), each task will take a day to complete, ask the maximum number of rewards?

 
Input
Multiple sets of test data. The first row is a number n, which indicates the total number of tasks. Next n lines, two numbers per line T and V, as described above.
Output
The highest reward for each set of data output.
Sample input
74 202 604 703 401 304 506 10
Sample output
230

This problem just started greedy did not use priority queue, is based on value, but feel wrong. Finally know to follow the days to greedy.

The code is as follows:

#include <stdio.h>#include<string.h>#include<iostream>#include<queue>#include<algorithm>using namespacestd;Const intN =50005;BOOLVis[n];structnode{intDay , value; FriendBOOL operator< (ConstNode A,ConstNode B) {        returnA.value > B.value;//value small with high priority    }};BOOLcmpConstNode A,ConstNode b)//level Two sorting, first in accordance with the number of days to row, the number of days in front, the same number of days in the case of reward size to row, reward the big in front{    if(A.day! =b.day)returnA.day <B.day; returnA.value >B.value;}intN; Node Node[n];intMain () { while(~SCANF ("%d", &n) &&N) { for(inti =0; I < n; i++) {scanf ("%d%d", &node[i].day, &node[i].value); } Sort (node, node+N, CMP); Priority_queue<Node>Q; memset (Vis,false,sizeof(VIS));  for(inti =0; I < n; i++)        {            if(!vis[node[i].day])//If there is no other mission to occupy this day{Vis[node[i].day]=true;            Q.push (Node[i]); }            Else            {                if(Q.size () < Node[i].day)//If there are free days ahead of this dayQ.push (Node[i]); Else{Node tmp= Q.top ();//compare it to the minimum value, if it is greater than the minimum value added to the queue, replace                    if(Tmp.value <node[i].value)                        {Q.push (node[i]);                    Q.pop (); }                }            }        }        Long LongAns =0;  while(!Q.empty ()) {Node tmp=Q.top ();            Q.pop (); Ans+=Tmp.value; } cout<< ans <<Endl; }    return 0;}
View Code

Nyoj 1107 Highest Reward (greedy + priority queue)

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.