HNOI2003 操作系统

题目链接

题解

中心思想就是模拟,堆按优先级大小排序,每次看堆顶元素是否完成,如果完成则就退出,否则等待下一个任务并减去这段等待时间,最后插入下一个任务,循环往复。
本题具有一定的实际意义。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
typedef struct{
int id,st,ls,pr;
}Pro;
Pro q[400005];
bool operator <(Pro a,Pro b){
if(a.pr==b.pr)return a.st>b.st;
else return a.pr<b.pr;
}
priority_queue<Pro> pq;
int at=0,r=0;
int s(Pro &x){
if(scanf("%d",&x.id)==1)
scanf("%d%d%d",&x.st,&x.ls,&x.pr);
else
return 0;
return 1;
}
int main(){
Pro pre,n,a;
while(s(q[r]))r++;
pq.push(q[0]);
at=q[0].st;
for(int i=1;i<r;i++){
while(!pq.empty()&&at+pq.top().ls<=q[i].st)
a=pq.top(),
printf("%d %d\n",a.id,at+a.ls),
at+=a.ls,
pq.pop();//若此项目已经完成,则退出
if(!pq.empty())
a=pq.top(),
pq.pop(),
a.ls-=q[i].st-at,
pq.push(a);//结束部分等待时间
at=q[i].st,
pq.push(q[i]);//放入优先队列
}
while(!pq.empty())
a=pq.top(),
printf("%d %d\n",a.id,at+a.ls),
at+=a.ls,
pq.pop();
return 0;
}