这道题比较明显的是一道优先队列+模拟的题,优先队列就用STL里的然后自定义一个类型就好,主要是模拟的过程,只有我们处理到下一个进程的到达时间晚于我们当前处理进程执行完的时间,我们才能够确定这个进程能被执行完。对于执行了部分时间的进程,我们维护它的剩余时间并不断更新优先队列的值,然后模拟一下就出来了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct Vergil
{
int num,kth,arr,tex;
bool operator < (const Vergil &a) const
{
return (kth<a.kth||(kth==a.kth&&arr>a.arr));
}
};
priority_queue<Vergil> q;
int t,a,b,c,d;
void add(int a,int b,int c,int d)
{
Vergil x;
x.num=a;x.arr=b;
x.tex=c;x.kth=d;
q.push(x);
}
int main()
{
scanf("%d%d%d%d",&a,&b,&c,&d);
add(a,b,c,d);
t=b;
while (scanf("%d%d%d%d",&a,&b,&c,&d)==4)
{
Vergil x=q.top();q.pop();
int v=x.tex;
bool flag=0;
while (t+v<=b)
{
printf("%d %d\n",x.num,t+v);
t+=v;
if (q.empty())
{
t=b;
flag=1;
break;
}
x=q.top();q.pop();
v=x.tex;
}
add(a,b,c,d);
if (flag) continue;
x.tex-=(b-t);
q.push(x);
t+=(b-t);
}
while (!q.empty())
{
Vergil x=q.top();q.pop();
int v=x.tex;
printf("%d %d\n",x.num,t+v);
t+=v;
}
return 0;
}