51nod 1163 Highest reward (greedy + priority queue)

Source: Internet
Author: User

Topic Links: 51nod 1163 the highest reward

Looking at this question I immediately think of yesterday also did a greedy plus priority queue of the problem.

Sort by the late end of a task, select a task, and then, if the task's late end is later than the current point, press the reward value of the task into the queue or replace the reward value of the smallest task in the queue, prioritizing the priority queue by a small reward value.

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 using namespacestd;6typedefLong Longll;7 Const intN =50001;8 structtask{9     inte;Ten     intW; One }a[n]; A BOOLCMP (Task x, task y) { -     returnX.E <y.e; - } thepriority_queue<int, vector<int, greater<int> >Q; - intMain () { -     intN, I, J; -ll ans =0; +scanf"%d", &n); -      for(i =0; I < n; ++i) +scanf"%d%d", &AMP;A[I].E, &A[I].W); ASort (A, A +N, CMP); at      for(i =0; I < n; ++i) { -         if(A[i].e >q.size ()) { -         //if the task's late end time is later than the current point in time - Q.push (A[I].W); -Ans + =A[I].W; -         } in         Else{ -             //may be replaced by a higher reward task toAns + =A[I].W; + Q.push (A[I].W); -Ans-=q.top (); the Q.pop (); *         } $     }Panax Notoginsengprintf"%lld\n", ans); -     return 0; the}
View Code

51nod 1163 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.