题目已经叫你模拟这个cpu做事。
进程有优先级,显然就弄一个优先队列,重载小于号别弄错了。
这道题的重点我觉得是在“进程被优先级高的进程中断”最难。
后来发现可以记录一个lastnow变量,表示目前的上一个任务是在什么时候完成的。
发现没有进程中途加入最容易弄了。
所以不如将每一次将中途加入之前的空隙模拟一遍,然后再把新进程添加。
发现可能会有进程做了一半,有一个手段:进程所需时间减掉即可。
其他的自己琢磨吧。。。
代码不长,比去年的D1T2短一大截。
有个坑点:等待队列的进程不超过15000,但总进程不止15000!数组开大点。
代码:
#include<cstdio>
#include<queue>
#include<algorithm>
const int maxn = 1500005;
struct Nodes
{
int id, t, tt, pri;
bool operator < (const Nodes &rhs) const
{
if(pri == rhs.pri) return t > rhs.t;
return pri < rhs.pri;
}
} a[maxn];
int n;
int lastok;
std::priority_queue<Nodes> q;
int main()
{
while(233)
{
n++;
if(scanf("%d%d%d%d", &a[n].id, &a[n].t, &a[n].tt, &a[n].pri) == EOF) break;
}
n--;
q.push(a[1]);
lastok = a[1].t;//imagine already have an element ok
for(int i = 2; i <= n; i++)
{
int now = a[i].t;
//solve situation [a[i - 1].t, a[i].t]
while(2333)//no element will add halfway
{
if(q.empty())
{
lastok = now;
break;
}
int ok = lastok + q.top().tt;
if(ok <= now)
{
printf("%d %d\n", q.top().id, ok);
lastok = ok;
q.pop();
}
else
{
Nodes temp = q.top();
temp.tt -= now - lastok;
q.pop();
q.push(temp);
lastok = now;
break;
}
}
q.push(a[i]);
}
while(!q.empty())
{
int ok = lastok + q.top().tt;
printf("%d %d\n", q.top().id, ok);
lastok = ok;
q.pop();
}
return 0;
}
原文:https://www.cnblogs.com/Garen-Wang/p/9379038.html