萌新求助 40pts
  • 板块P9518 queue
  • 楼主T1EM1E
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/8/12 19:56
  • 上次更新2023/11/3 04:13:16
查看原帖
萌新求助 40pts
643058
T1EM1E楼主2023/8/12 19:56
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define mod 1000000007
#define bug(x) cout<<"Bug "<<(x)<<endl
#define el putchar('\n')
#define sp putchar(' ')
using namespace std;
inline int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0' || ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9') s=s*10+ch-'0',ch=getchar();
	return s*w;
}
inline void write(int x)
{
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+'0');
	return;
}
const int N=1e5+10;
int n;
queue<string> q;
map<string,int> mp;//is in queue and calc?
map<string,int> waiting;//is in queue?
map<string,int> gaming;//is in game?
string last1,last2;
void leave(string t,bool f,bool force);
void add(string t,bool f);
void start()
{
	if(last1!="")
	{
		leave(last1,0,1);add(last1,0);
		gaming[last1]=0;
	}
	if(last2!="")
	{
		leave(last2,0,1);add(last2,0);
		gaming[last2]=0;
	}
	last1="";last2="";
	if(q.empty())
	{
		printf("Error\n");
		return ;
	}
	int cnt=0;
	while(cnt!=2)
	{
		string s=q.front();q.pop();
		
		if(mp[s])
		{
			printf("%s ",s.c_str());
			waiting[s]=0;gaming[s]=1;
			if(cnt==0) last1=s;
			else last2=s;
			cnt++;
		}
		if(q.empty())
		{
			if(cnt==0)
			{
				printf("Error\n");
				return ;
			}
			break;
		}
	}
	el;
	return ;
}
void add(string t,bool f)
{
	if(mp[t])
	{
		if(f) printf("Error\n");
		return ;
	}
	if(!waiting[t]) q.push(t);
	mp[t]=1;waiting[t]=1;
	if(f) printf("OK\n");
	return ;
}
void leave(string t,bool f,bool force)
{
	if((!mp[t] || gaming[t]) && !force)
	{
		if(f) printf("Error\n");
		return ;
	}
	mp[t]=0;
	if(f) printf("OK\n");
	return ;
}
int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++)
	{
		// if(!q.empty()) bug(q.front());
		string a,b;
		cin>>a;
		if(a[0]=='s') start();
		else if(a[0]=='a')
		{
			cin>>b;
			add(b,1);
		}
		else if(a[0]=='l')
		{
			cin>>b;
			leave(b,1,0);
		}
	}
	return 0;
}
2023/8/12 19:56
加载中...