维护两队列。
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
class node1
{
public :
int time,pth;
};
bool operator< (const node1 &a,const node1 &b)
{
return a.pth >b.pth ;
}
class node2
{
public :
int time,pth;
};
bool operator <(const node2 &a,const node2 &b)
{
return a.time >b.time ;
}
priority_queue<node1>prepare;
priority_queue<node2>fire;
int state[30005];
int n,t,p;
void update(int time)
{
while(!fire.empty ()&&fire.top ().time +t<=time)
{
if(fire.top().time !=state[fire.top ().pth ])
fire.pop ();
else
{
state[fire.top ().pth ]=-1;
node1 a;
a.pth =fire.top ().pth ;a.time =0;
fire.pop ();
prepare.push (a);
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&t,&p)!=EOF)
{
t*=60;
while(!prepare.empty ()) prepare.pop ();
while(!fire.empty ()) fire.pop ();
for(int i=1;i<=n;i++)
{
node1 x;x.pth =i;x.time =0;
prepare.push (x); state[i] =-1;
}
while(p--)
{
int time,k;
char ch;
scanf("%d %c",&time,&ch);
update(time);
if(ch=='p')
{
printf("%d\n",prepare.top ().pth );
state[prepare.top ().pth ]= time;
node2 b;
b.pth =prepare.top ().pth ;b.time =time;
prepare.pop ();
fire.push (b);
}
else
{
scanf("%d",&k);
if(state[k]==-1)
printf("Cannot\n");
else
{
printf("Fire!\n");
state[k]=time;
node2 b;
b.pth =k;b.time =time ;
fire.push (b);
}
}
}
}
return 0;
}