UVa 1153 Keep the Customer satisfied (greedy + priority queue)

Source: Internet
Author: User

Test instructions: Given n jobs, it is known that each job will take the time Q and the cut-off time D, asking you how many jobs you can do up to a maximum of one job at a time.

Analysis: This problem is greedy, should be able to see, the key is greedy strategy is what, so think, first by the cut-off time sort, then so, all the work is in accordance with the deadline row, because we first guarantee that,

Cut-off time before the first election, and then from all the traversal, the use of priority queue, Q big priority, and then consider, the back, if the back can also be completed within the deadline, put, if not, then,

and the queue q the longest ratio, if it is longer than the queue Q the longest, then do not, otherwise, then delete the longest, put it in, think why, because, if so, then the number of work does not decrease,

But there is more time left to finish the rest. If you do not know the priority queue Click Http://www.cnblogs.com/dwtfukgv/articles/5640285.html

Note that there was a pit that I had been in for several days .... That is the Q may be bigger than D, this is not to be considered .....

The code is as follows:

#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <set > #include <cstring> #include <cmath> #include <queue>using namespace std;const int maxn = 8e5 + 5;stru    CT node{int q, D;    BOOL operator < (const node &AMP;P) const{//Priority Queue Maximum priority return Q < P.Q; }};node a[maxn];int n;bool CMP (const node &AMP;P, const node &AMP;QQ) {//Sort, return by cutoff time P.D < QQ.D | | (P.D = = qq.d && p.q < QQ.Q);}    int solve () {priority_queue<node> PQ;    int s = 0;   for (int i = 0; i < n; ++i) if (a[i].q <= a[i].d)//Note that the work itself is problematic if (Pq.empty ()) {Pq.push (a[i]);  s = a[i].q;  } else if (S + a[i].q <= a[i].d) {s + = A[I].Q;  Pq.push (A[i]);  }//can be completed else{node U = pq.top () before the cutoff time;            Pq.pop ();  if (u.q > a[i].q && s-u.q + a[i].q <= a[i].d) {Pq.push (a[i]); S-= U.Q-A[I].Q;        }//q the longest small else pq.push (U) than the queue; } return Pq.size ();}int Main () {//Freopen ("In.txt", "R", stdin);  int T;    Cin >> T;        while (t--) {scanf ("%d", &n);        for (int i = 0; i < n; ++i) scanf ("%d%d", &AMP;A[I].Q, &AMP;A[I].D);        Sort (A, a+n, CMP);//Sort printf ("%d\n", Solve ());    if (T) printf ("\ n"); } return 0;}

UVa 1153 Keep the Customer satisfied (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.