题意
输入输出
思路
用优先队列维护等待进程,同时用两个变量分别维护当前时间和当前进程。
每当一个新进程到来时,看看再其到来之前,又有哪些进程执行完毕。
然后再与当前优先级最高的进程比较优先级,进行简单的分类讨论。
最后不要忘记,所有进程都进来之后,再将他们从优先队列中弹出,直到优先队列为空为止。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct Process
{
int id, gettime, exetime, importance;
int starttime;
bool operator < (const Process &p) const
{
if(importance != p.importance) return importance < p.importance;
return gettime > p.gettime;
}
};
typedef struct Process process;
int main()
{
int a, b, c, d;
priority_queue<process> heap;
int time = 0;
scanf("%d%d%d%d", &a, &b, &c, &d);
process p = {a, b, c, d, b};
process tmp = p;
heap.push(p);
while(~scanf("%d%d%d%d", &a, &b, &c, &d)) {
process p = {a, b, c, d};
while(heap.size() && tmp.starttime + tmp.exetime <= p.gettime) {
printf("%d %d
", tmp.id, tmp.starttime + tmp.exetime);
int t = tmp.starttime + tmp.exetime;
heap.pop();
if(heap.size()) {
tmp = heap.top();
tmp.starttime = t;
}
}
if(heap.size()) {
tmp.exetime -= p.gettime - tmp.starttime;
tmp.starttime = p.gettime;
}
time = p.gettime;
if(!heap.size()) {
p.starttime = time;
heap.push(p);
tmp = p;
continue;
}
if(p < tmp) {
heap.push(p);
tmp.exetime -= time - tmp.starttime;
tmp.starttime = time;
heap.pop();
heap.push(tmp);
}
else {
tmp.exetime -= time - tmp.starttime;
heap.pop();
heap.push(tmp);
p.starttime = time;
heap.push(p);
tmp = p;
}
}
while(heap.size()) {
tmp = heap.top();
tmp.starttime = time;
printf("%d %d
", tmp.id, tmp.starttime + tmp.exetime);
time = tmp.starttime + tmp.exetime;
heap.pop();
}
return 0;
}