就是堆的应用,实现的时候会有点奇怪的技巧
now作为当前时间,顺序枚举每一个进程的到来,注意在每一个进程来之前要处理完成前面的所有进程!
注意放一个哨兵元素lev为-inf,rest为inf,防止bug,
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
inline int read()
{
int ans,f=1;char ch;
while ((ch=getchar())<'0'||ch>'9') if (ch=='-') f=-1;ans=ch-'0';
while ((ch=getchar())>='0'&&ch<='9') ans=ans*10+ch-'0';
return ans*f;
}
struct aa
{
int id,ar,rest,lev;
bool operator <(const aa &b) const
{
if (lev==b.lev) return ar>b.ar;
else return lev<b.lev;
}
}a[800005];
int tot;
priority_queue<aa> heap;
int main()
{
int x,y,z,w;
while (scanf("%d%d%d%d",&x,&y,&z,&w)!=EOF)
{
tot++;
a[tot].id=x;a[tot].ar=y;
a[tot].rest=z;a[tot].lev=w;
}
int now=0;
aa tmp;
tmp.rest=inf;
tmp.lev=-inf;
heap.push(tmp);
for (int i=1;i<=tot;i++)
{
int now1=a[i].ar;
while (heap.top().lev!=-inf&&heap.top().rest<=now1-now)
{
now+=heap.top().rest;
printf("%d %d\n",heap.top().id,now);
heap.pop();
}
aa v=heap.top();
heap.pop();
v.rest-=now1-now;
heap.push(v);
heap.push(a[i]);
now=now1;
}
while (heap.top().lev!=-inf)
{
now+=heap.top().rest;
printf("%d %d\n",heap.top().id,now);
heap.pop();
}
return 0;
}