Code:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<unordered_map>
#include<queue>
using namespace std;
int n;
string q[1000010];
int head , tail = -1;
unordered_map<string , bool> now;
unordered_map<string , bool> play;
int k = 0;
int main(){
cin >> n;
while(n--){
string str;
cin >> str;
string x;
if(str == "start"){
if(head > tail) puts("Error");
else{
while(head <= tail){
if(now[q[head]]){
if(play[q[head]]){
play[q[head]] = 0;
q[++tail] = q[head++];
}
else break;
}
else head++;
}
// for(int i = 0;head + i <= tail && i <= 1 && now[q[head + i]];i++){
// cout << q[head + i] << " ";
// play[q[head + i]] = 1;
// }
int p = 0 , cnt = 0;
while(head + p <= tail && cnt <= 1){
if(now[q[head + p]]){
cout << q[head + p] << " ";
play[q[head + p]] = 1;
p++; cnt++;
}
else p++;
}
if(!cnt) puts("Error");
else cout << endl;
}
}
else if(str == "arrive"){
cin >> x;
if(!now[x] && !play[x]){
cout << "OK" << endl;
now[x] = 1;
q[++tail] = x;
}
else if(now[x] || play[x]) puts("Error");
}
else{
cin >> x;
if(!now[x]) puts("Error");
else if(play[x]) puts("Error");
else{
cout << "OK" << endl;
now[x] = 0;
}
}
}
return 0;
}