Luo Valley P2278 [HNOI2003] operating system (analog, priority queue)

Portal


Problem-solving ideas

The first is the practice of violence :

From the beginning enumeration time, plus each time a judge has no new task arrives, then pick out the biggest priority value priority queue, perform a second, back in the queue.

Apparently TLE's!

Then think about optimization:

Where you can look for optimization, we found that only time! So every time we remove the head of the queue (task a) There are two cases - the first time the task is now time + a remaining time to complete the work required <next to arrive, this time the optimal strategy must be this is a complete piece of work; the second is to finish the job, do it as much as possible, the time required to subtract a surplus of time to do, and then back into the queue.

According to that when a new mission did not arrive, the best is always the first team, at that moment a new task arrives, not necessarily, because the new element may be higher than the team's first priority.

note

  • Weight carrying structure operator wording
  • Comparison can not be completed is not less than or less

AC Code

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<queue>
 7 using namespace std;
 8 struct node{
 9     int num,t1,t2,d;
10     bool operator < (const node &x)const{
11         return d==x.d?t1>x.t1:d<x.d;
12     }
13 }x;
14 priority_queue<node> q;
15 int nowt;
16 int main()
17 {
18     while(scanf("%d",&x.num)!=EOF){
19         scanf("%d%d%d",&x.t1,&x.t2,&x.d);
20         if(q.empty()){
21             q.push(x);
22             nowt=x.t1;
23             continue;
24         }
25         node f=q.top();
26         while(nowt+f.t2<=x.t1){
27             q.pop();
28             nowt=nowt+f.t2;
29             printf("%d %d\n",f.num,nowt);
30             if(q.empty()) break;
31             f=q.top();
32         }
33         if(q.empty()){
34             nowt=x.t1;
35             q.push(x);
36             continue;
37         }
38         f.t2-=x.t1-nowt;
39         q.pop();
40         q.push(f);
41         nowt=x.t1;
42         q.push(x); 
43     }
44     while(!q.empty()){
45         node f=q.top();
46         q.pop();
47         nowt=nowt+f.t2;
48         printf("%d %d\n",f.num,nowt);
49     }
50     return 0;
51 }

// HNOI2003 Day1t2

Guess you like

Origin www.cnblogs.com/yinyuqin/p/12104064.html