三个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;
}