70pts求调
  • 板块P9518 queue
  • 楼主Ariasaka
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/8/12 19:16
  • 上次更新2023/11/3 04:13:47
查看原帖
70pts求调
761305
Ariasaka楼主2023/8/12 19:16

可能是一些细节错误,考场调了很久从40pts到70pts,但是好像还是有问题,写了一个队列+懒删除标记:

#include <bits/stdc++.h>
using namespace std;
int n,len;
string op,name,p1="",p2="";
queue<string> q;
unordered_map<string,int> del,vis;
int main(){
	ios::sync_with_stdio(0);
	cin>>n;
	while(n--){
		cin>>op;
		if(op=="arrive"){
			cin>>name;
			if(!vis[name]&&p1!=name&&p2!=name)
				q.push(name),len++,
				vis[name]=1,cout<<"OK\n";
			else cout<<"Error\n";
		}
		else if(op=="leave"){
			cin>>name;
			if(vis[name]&&p1!=name&&p2!=name)
				del[name]++,len--,
				vis[name]=0,cout<<"OK\n";
			else cout<<"Error\n";
		}
		else if(op=="start")
			if(len){
				if(p1!="") q.push(p1),vis[p1]=1,len++;
				if(p2!="") q.push(p2),vis[p2]=1,len++;
				while(1){
					string tmp=q.front();
					if(del[tmp]) q.pop(),del[tmp]--;
					else break;
				}
				p1=q.front(),vis[p1]=0,q.pop(),len--;
				if(len){
					while(1){
						string tmp=q.front();
						if(del[tmp]) q.pop(),del[tmp]--;
						else break;
					}
					p2=q.front(),vis[p2]=0,q.pop(),len--;
				}
				else p2="";
				cout<<p1<<" "<<p2<<"\n";
			}
			else cout<<"Error\n";
	}
	return 0;
}
2023/8/12 19:16
加载中...