80pts 求优化
  • 板块P9518 queue
  • 楼主xiaoyang111
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/9/17 10:21
  • 上次更新2023/11/2 19:52:16
查看原帖
80pts 求优化
890311
xiaoyang111楼主2023/9/17 10:21

三个TLE,所以说STL链表能过吗。

代码:

#include <iostream>
#include <list>
#include <unordered_map>
using namespace std;
inline string readstr(){
	string s="";
	char ch;
	while ((ch=getchar())!='\n' && ch!=' ') {
		s+=ch;
	}
	return s;
}
int main(){
	list<string> l;
	unordered_map<string,bool> m;
	int n;
	scanf("%d",&n);
	string t1="12345678910",t2="12345678910";
	char t=getchar();
	for (int _=0;_<n;++_){
		string str=readstr();
		if (str=="start"){
			if (t1!="12345678910"){
				l.emplace_back(t1);
				m[t1]=true;
				t1="12345678910";
				if (t2!="12345678910"){
					l.emplace_back(t2);
					m[t2]=true;
					t2="12345678910";
				}
			}
			if (l.size()==0){
				printf("Error\n");
			}
			if (l.size()==1){
				t1=l.front();
				l.pop_front();
				m[t1]=false;
				cout<<t1<<'\n';
			}else if (l.size()>=2){
				t1=l.front();
				m[t1]=false;
				l.pop_front();
				t2=l.front();
				m[t2]=false;
				l.pop_front();
				cout<<t1<<" "<<t2<<'\n';
			}
		}
		if (str=="arrive"){
			string x=readstr();
			if (m[x] || x==t1 || x==t2){
				printf("Error\n");
			}else{
				l.emplace_back(x);
				m[x]=true;
				printf("OK\n");
			}
		}
		if (str=="leave"){
			string x=readstr();
			if (m[x]){
				l.remove(x);//这个地方怎么优化
				m[x]=false;
				printf("OK\n");
			}else{
				printf("Error\n");
			}
		}
	}
	return 0;
}

记录

2023/9/17 10:21
加载中...