Basic Data Structure
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 not change 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 not change 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:
0
Hint
In 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.NAND运算 只有 两个数同时为1 时运算结果为0 只要找到离栈顶最远的那个0就好
开始的时候单纯用双端队列做这题,结果搞了一中午 还是GG了 最后只得用数组模拟 双端队列记录0的位置
开数组模拟,用双端队列记录所有0的位置
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 char str[10]; 6 int data[450000]; 7 8 deque<int> q; 9 10 //* 11 int main() 12 { 13 14 //开数组模拟 REVERSE 15 deque<int> q; 16 int T, N, cnt = 0; 17 bool flag = false; 18 scanf("%d",&T); 19 for(int i = 1; i <= T; i++) 20 { 21 q.clear(); cnt = 0; 22 printf("Case #%d: ",i); 23 int h = 200001, t = 200000, num; 24 scanf("%d",&N); 25 while(N--) 26 { 27 scanf("%s",str); 28 if(str[2] == 'S')//PUSH 29 { 30 scanf("%d",&num); 31 if(flag)//REVERSE 32 { 33 data[t--] = num;//数组向左插入数据,模拟栈 34 if(!num)//num == 0 35 q.push_front(t+1); 36 } 37 else 38 { 39 data[h++] = num; 40 if(!num) 41 q.push_back(h-1); 42 } 43 cnt++;//模拟栈中数据元素的个数 44 } 45 else if(str[2] == 'P')//POP 46 { 47 if(cnt ==0) 48 continue; 49 cnt--; 50 if(flag) 51 { 52 if(data[++t] == 0) 53 q.pop_front();//如果栈顶为0,将双端队列顶端的0删除 54 } 55 else 56 { 57 if(data[--h] == 0) 58 q.pop_back(); 59 } 60 } 61 else if(str[2] == 'V')//REVERSE 62 flag = !flag;//REVERSE 操作 63 else//QUERY 64 { 65 int num = 0; 66 if(cnt == 0)//栈中没有元素 67 printf("Invalid. "); 68 else if(cnt == 1)//栈中只有一个元素 69 printf("%d ",data[h-1]);//data[t+1] 70 else 71 { 72 if(flag) 73 { 74 if(q.empty()) 75 num = cnt;//双端队列为空,即模拟栈中全为1 76 else 77 num = q.back() == t+1 ? cnt - 1 : h - q.back();//找距离栈顶最远的 0, t+1 此时模拟栈中栈顶, 不能直接 h - q.back(),下同 78 } 79 else 80 { 81 if(q.empty()) 82 num = cnt; 83 else 84 num = q.front() == h - 1 ? cnt -1 :q.front()- t;//距离最远的0 h-1 若没有翻转 栈顶位置 85 } 86 if(num % 2) 87 printf("1 "); 88 else 89 printf("0 "); 90 } 91 } 92 } 93 } 94 return 0; 95 }//*/
恩,留下图,方便理解