Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 260 Accepted Submission(s): 62
Problem Description
Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack:
∙ PUSH x: put x on the top of the stack, x must be 0 or 1.
∙ POP: throw the element which is on the top of the stack.
Since it is too simple for Mr. Frog, a famous mathematician who can prove "Five points coexist with a circle" easily, he comes up with some exciting operations:
∙ REVERSE: Just reverse the stack, the bottom element becomes the top element of the stack, and the element just above the bottom element becomes the element just below the top elements... and so on.
∙ QUERY: Print the value which is obtained with such way: Take the element from top to bottom, then do NAND operation one by one from left to right, i.e. If atop,atop−1,⋯,a1 is corresponding to the element of the Stack from top to the bottom, value=atop nand atop−1 nand ... nand a1 . Note that the Stack will notchange after QUERY operation. Specially, if the Stack is empty now,you need to print ” Invalid.”(without quotes).
By the way, NAND is a basic binary operation:
∙ 0 nand 0 = 1
∙ 0 nand 1 = 1
∙ 1 nand 0 = 1
∙ 1 nand 1 = 0
Because Mr. Frog needs to do some tiny contributions now, you should help him finish this data structure: print the answer to each QUERY, or tell him that is invalid.
∙ PUSH x: put x on the top of the stack, x must be 0 or 1.
∙ POP: throw the element which is on the top of the stack.
Since it is too simple for Mr. Frog, a famous mathematician who can prove "Five points coexist with a circle" easily, he comes up with some exciting operations:
∙ REVERSE: Just reverse the stack, the bottom element becomes the top element of the stack, and the element just above the bottom element becomes the element just below the top elements... and so on.
∙ QUERY: Print the value which is obtained with such way: Take the element from top to bottom, then do NAND operation one by one from left to right, i.e. If atop,atop−1,⋯,a1 is corresponding to the element of the Stack from top to the bottom, value=atop nand atop−1 nand ... nand a1 . Note that the Stack will notchange after QUERY operation. Specially, if the Stack is empty now,you need to print ” Invalid.”(without quotes).
By the way, NAND is a basic binary operation:
∙ 0 nand 0 = 1
∙ 0 nand 1 = 1
∙ 1 nand 0 = 1
∙ 1 nand 1 = 0
Because Mr. Frog needs to do some tiny contributions now, you should help him finish this data structure: print the answer to each QUERY, or tell him that is invalid.
Input
The first line contains only one integer T (
T≤20
), which indicates the number of test cases.
For each test case, the first line contains only one integers N ( 2≤N≤200000 ), indicating the number of operations.
In the following N lines, the i-th line contains one of these operations below:
∙ PUSH x (x must be 0 or 1)
∙ POP
∙ REVERSE
∙ QUERY
It is guaranteed that the current stack will not be empty while doing POP operation.
For each test case, the first line contains only one integers N ( 2≤N≤200000 ), indicating the number of operations.
In the following N lines, the i-th line contains one of these operations below:
∙ PUSH x (x must be 0 or 1)
∙ POP
∙ REVERSE
∙ QUERY
It is guaranteed that the current stack will not be empty while doing POP operation.
Output
For each test case, first output one line "Case #x:w, where x is the case number (starting from 1). Then several lines follow, i-th line contains an integer indicating the answer to the i-th QUERY operation. Specially, if the i-th QUERY is invalid, just print "
Invalid."(without quotes). (Please see the sample for more details.)
Sample Input
2 8 PUSH 1 QUERY PUSH 0 REVERSE QUERY POP POP QUERY 3 PUSH 0 REVERSE QUERY
Sample Output
Case #1: 1 1 Invalid. Case #2: 0HintIn the first sample: during the first query, the stack contains only one element 1, so the answer is 1. then in the second query, the stack contains 0, l(from bottom to top), so the answer to the second is also 1. In the third query, there is no element in the stack, so you should output Invalid.
思路:开40万的数组分别向两端扩展,记录所有0的位置,每次询问时末尾是0的段得到值肯定是1,或者0是首项则得到0,再判断0之后的1的个数的奇偶性即可,由于出栈时可能左右会出栈到另一端,所以使用双端队列(其实我觉得就是双端栈。。。。)
#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;
}