中山大学 1238: 兵临城下//优先队列

维护两队列。

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值