#include <iostream>
using namespace std;
//注意head>tail 和 head<tail时入栈出栈的下标+-不同
long long st[600002]={0};
int head=300000,tail=300000;
int main()
{
int q;
cin>>q;
for(int i=0;i<q;i++){
int cmd;
cin>>cmd;
if(cmd==1){
long long X;
cin>>X;
if(head>=tail){
st[head++]=X;
}
else{
st[head--] = X;
}
cout<<X<<endl;
}
else if(cmd==2){
if(head>=tail){
head--;
if(head==tail) cout<<-1<<endl;
else cout<<st[head-1]<<endl;
}
else{
head++;
if(head==tail) cout<<-1<<endl;
else cout<<st[1+head]<<endl;
}
}
else{
if(head==tail) cout<<-1<<endl;
else if(head>tail){
int tmp = head-1;
head = tail-1;
tail = tmp;
cout<<st[1+head]<<endl;
}
else{
int tmp = head+1;
head = tail+1;
tail = tmp;
cout<<st[head-1]<<endl;
}
}
}
return 0;
}
EOJ_1021_可旋栈
最新推荐文章于 2021-10-27 20:00:33 发布