P2278 操作系统
感谢所有AC
传送门
思路
模拟题,没什么思路。
不过代码能力真的有待提高!!!
代码
#include
#include
using namespace std;
struct node {
int id, start, rest, prior;
bool operator<(const node& a)const
{
if (prior != a.prior) return prior < a.prior;
else return start > a.start;
}
}newin;
priority_queue cpu;
long long nowtime;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
while (cin >> newin.id >> newin.start >> newin.rest >> newin.prior)
{
//先判断新的进程是否干扰到正在进行的进程
//未干扰进程,继续执行
while (!cpu.empty() && (nowtime + cpu.top().rest <= newin.start))
{
node ok = cpu.top();
cpu.pop();
nowtime += ok.rest;
cout << ok.id << ' ' << nowtime << '\n';
}
//干扰进程,新进程入堆
//注意这边可能新进程的优先级没有比正在执行的高
//可以直接放入cpu中,cpu会自己处理优先级
if (!cpu.empty())
{
node half = cpu.top();
cpu.pop();
half.rest = nowtime + half.rest - newin.start;
cpu.push(half);
}
cpu.push(newin);
nowtime = newin.start;
}
//没有新进程,执行等待的进程
while (!cpu.empty())
{
node ok = cpu.top();
cpu.pop();
nowtime += ok.rest;
cout << ok.id << ' ' << nowtime << '\n';
}
return 0;
}