<pre name="code" class="html">#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int a[450000];
deque<int> q;
int main()
{
int T;
scanf("%d",&T);
int ca=1;
while(T--)
{
int h=200001;
int t=h-1;
int type=1;
int n;
scanf("%d",&n);
char op[10];
q.clear();
int cnt=0;
printf("Case #%d:\n",ca++);
for(int i=0; i<n; i++)
{
scanf("%s",op);
int x;
if(op[0]=='P'&&op[1]=='U')
{
scanf("%d",&x);
if(type)
{
a[h]=x;
if(!x)
q.push_back(h);
h++;
}
else
{
a[t]=x;
if(!x)
q.push_front(t);
t--;
}
cnt++;
}
else if(op[0]=='P'&&op[1]=='O')
{
if(cnt==0)
continue;
if(type)
{
if(a[h-1]==0)
q.pop_back();
h--;
}
else
{
if(a[t+1]==0)
q.pop_front();
t++;
}
cnt--;
}
else if(op[0]=='Q')
{
int num=0;
if(cnt==0)
{
cout<<"Invalid."<<endl;
}
else if(cnt==1)
{
cout<<a[h-1]<<endl;
}
else
{
if(type)
{
if(q.empty())
num=cnt;
else
{
num=q.front()==h-1?cnt-1:q.front()-t;
}
}
else
{
if(q.empty())
num=cnt;
else
{
num=q.back()==t+1?cnt-1:h-q.back();
}
}
if(num%2==0)
cout<<"0"<<endl;
else
cout<<"1"<<endl;
}
}
else
{
type^=1;
}
}
}
return 0;
}
tjut 5929
最新推荐文章于 2016-11-04 22:02:44 发布