题解:洛谷P2278 [HNOI2003] 操作系统

由于太弱开始写一些绿题及以上的单题题解

说实话我觉得这身为一道蓝题并不是非常的难qwq

洛谷


思路

题目明摆着“选择优先级最高的先运行”,于是非常一眼的优先队列

直接蠢蠢地模拟即可

(STL模拟水题真好玩(?

代码

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>

using namespace std;

struct node
{
int id,st,len,lv; //任务序号,开始时间,长度,优先级,就是输入的部分

bool operator <(const node &a) const //先按优先级排列,否则按开始时间,题目要求
{
if(lv==a.lv)
{
return st>a.st;
}
else
{
return lv<a.lv;
}
}
} tsk;

long long tot;
priority_queue<node>q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);

while(cin>>tsk.id>>tsk.st>>tsk.len>>tsk.lv)
{
//可以在这个任务开始之前做完的任务先按优先级做完
while(!q.empty()&&tot+q.top().len<=tsk.st)
{
node t=q.top();
q.pop();
tot+=t.len;
cout<<t.id<<" "<<tot<<"\n";
}

//如果还有未做的任务不能做完,先做一半,再放回去
//下一轮循环,如果该任务仍然优先级最高,就继续做
if(!q.empty())
{
node t=q.top();
q.pop();
t.len=t.len-tsk.st+tot;
q.push(t);
}

q.push(tsk); //将新任务放进队列
tot=tsk.st;
}

//将剩余任务做完
while(!q.empty())
{
node t=q.top();
q.pop();
tot+=t.len;
cout<<t.id<<" "<<tot<<"\n";
}

return 0;
}

就没了。

蓝题欸。。。

题解:洛谷P2278 [HNOI2003] 操作系统

https://imoliviauu.github.io/2024/04/28/solution-luogu-p2278/

作者

Olivia_uu

发布于

2024-04-28

更新于

2024-05-11

许可协议

You need to set install_url to use ShareThis. Please set it in _config.yml.

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×