LA 4254 Processor Processor "Two-point greedy priority queue"

Source: Internet
Author: User

Topic Links:

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21663

Two points:

  The problem of minimizing the maximum value is solved by two points. After two minutes of processor speed, the problem turns into the question of whether the processor can handle these problems for a given processing speed.

Greedy:

A greedy idea is that every moment should try to do the task, the end of the earliest time, so at least not make the result worse. This allows you to enumerate each unit of time, and then go to the one that can be done and the oldest end time to do so until you run out of this unit of time or have no tasks to do. Finally, if you find out which task is not finished, it means that at this given processing speed, the processor is unable to complete all tasks.

Priority queue:

The overall idea is to sort all tasks by the start time, two to enumerate the speed of the processor, and then to judge each speed. The priority queue is followed by the task start time, and the priority of the queue is the early end time. Finally, you can determine whether the speed is capable of performing all tasks.

1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <string>7#include <vector>8#include <Set>9#include <queue>Ten#include <map> One#include <stack> A using namespacestd; - #defineCLR (c) memset (c, 0, sizeof (c)); - Const intINF =0x3f3f3f3f; thetypedefLong Longll; -typedef unsignedLong Longull; -typedefstructjob{ -     intL, R, W; +     BOOL operator< (Constjob& j)Const{//for priority queue internal sorting -         returnR > J.R;//r Large priority is low, priority queue first outputs a large priority member +     } A     BOOL operator> (Constjob& j)Const{ at         returnJ < * This; -     } - }job; -  - BOOLCMP (Job J1, job J2) {//for the sort function -     returnJ1.L < J2.L;//When True indicates that the comparison result meets the requirements, no swap location is required in } - Const intMAXL =10005; to Job J[MAXL]; + intT, N, Begin, End, Maxs, L, R, Mid, ans; - BOOLOk (intSpeed ) { thePriority_queue<job>Q; *     intpos =0; $      for(inti = Begin; I <= End; i++){Panax Notoginseng         intTime =Speed ; -          while(J[pos].l < i && POS < n) Q.push (j[pos++]); the          while(! Q.empty () && Time! =0){ +Job jtemp =q.top (); A Q.pop (); the             if(JTEMP.R < i)return false;//at this point the task's execution period has elapsed, stating that this speed cannot complete the execution of the task +             if(Jtemp.w >Time ) { -JTEMP.W-=Time ; $Time =0; $Q.push (jtemp);//perform part of the task, this task is not completed partially re-queued -             } -             ElseTime-= JTEMP.W;//This task completes and the next task is executed with the remaining time the         } -     }Wuyi     if(Q.empty ())return true; the     Else return false; - } Wu  - intMain () { Aboutscanf"%d", &T); $      while(t--){ -Begin = INF, End =0, Maxs =0; -scanf"%d", &n); -          for(inti =0; I < n; i++){ Ascanf"%d%d%d", &AMP;J[I].L, &AMP;J[I].R, &J[I].W); +begin = min (begin, J[I].L);//earliest start time for all Tasks theend = Max (end, J[I].R);//The latest end time for all Tasks -Maxs + = J[I].W;//Maximum speed: All tasks must be completed in 1 unit time, and the speed should be greater than or equal to the workload of all tasks and $         } theSort (J, j+N, CMP); theL =0, r = maxs, ans =INF; the          while(L <= R) {//two points theMid = (L + r) >>1;//(L + R)/2; -             if(Ok (mid)) { inR = Mid-1;//looking for the smallest of all the speeds that fit the requirements. theAns =min (ans, mid); the             } About             ElseL = mid +1;//If the current speed does not meet the requirements, you must increase the speed the         } theprintf"%d\n", ans); the     } +  -     return 0; the}

LA 4254 Processor Processor "Two-point 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.