蒟蒻求助,为什么这个代码80分啊,TLE了最后两个点,是没有特判吗?
  • 板块P9518 queue
  • 楼主zk_y
  • 当前回复5
  • 已保存回复5
  • 发布时间2023/8/12 22:11
  • 上次更新2023/11/3 04:11:15
查看原帖
蒟蒻求助,为什么这个代码80分啊,TLE了最后两个点,是没有特判吗?
792089
zk_y楼主2023/8/12 22:11
#include<iostream>
#include<queue>
#include<map>
using namespace std;
const int N=1e5;
queue<string> way;
int n;
string player1=" ",player2=" ";
string s;
map<string,bool> vis;
void start(){
	if(player1!=" ")way.push(player1);
	if(player2!=" ")way.push(player2);
	if(way.size()>=2){
		cout<<way.front()<<" ";
		player1=way.front();
		way.pop();
		cout<<way.front()<<'\n';
		player2=way.front();
		way.pop();
		return;
	}
	else if(way.size()==1){
		cout<<way.front()<<"\n";
		player1=way.front();
		way.pop();
		player2=" ";
		return;
	}
	else if(way.size()==0){
		player1=player2=" ";
		cout<<"Error\n";	
	}
}
void arrive(string a){
	if(vis[a])cout<<"Error\n";
	else{
		way.push(a);
		vis[a]=1;
		cout<<"OK\n";
	}
}
void left(string a){
	string k=way.front();
	if(k!=a){
		while(way.front()!=a){
			way.push(way.front());
			way.pop();
		}
		way.pop();
		while(way.front()!=k){
			way.push(way.front());
			way.pop();
			if(way.front()==k)return;
		}
	}
	if(k==a){
		way.pop();
	}
}
void leave(string a){
	if(player1==a||player2==a||!vis[a]){
		cout<<"Error\n";
		return;
	}
	else{
		vis[a]=0;
		left(a);
		cout<<"OK\n";
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n;
	while(n--){
		cin>>s;
		if(s=="start"){
			start();
		}
		else if(s=="arrive"){
			cin>>s;
			arrive(s);
		}
		else if(s=="leave"){
			cin>>s;
			leave(s);
		}
	}
	return 0;
}

是不是没有特判啊?最后两个点名字是只有大写字母,要特判吗,还是有更好的优化

2023/8/12 22:11
加载中...