Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 78 Accepted Submission(s): 12
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.题意:维护一个栈,支持往栈里塞 0/1 ,弹栈顶,翻转栈,询问从栈顶到栈底按顺序 NAND 的值。
题解:只要知道最后的
0
后面 1
的个数的奇偶性就行。我这里是用set 存储0的位置 比较麻烦 也可以和存储0/1一样 维护一个 0的位置 的栈
感谢评论区 hack数据
1
10
PUSH 0
REVERSE
QUERY
PUSH 1
PUSH 1
REVERSE
POP
QUERY
POP
QUERY
10
PUSH 0
REVERSE
QUERY
PUSH 1
PUSH 1
REVERSE
POP
QUERY
POP
QUERY
代码中已经标记更改
1 /****************************** 2 code by drizzle 3 blog: www.cnblogs.com/hsd-/ 4 ^ ^ ^ ^ 5 O O 6 ******************************/ 7 #include<bits/stdc++.h> 8 #include<map> 9 #include<set> 10 #include<cmath> 11 #include<queue> 12 #include<bitset> 13 #include<math.h> 14 #include<vector> 15 #include<string> 16 #include<stdio.h> 17 #include<cstring> 18 #include<iostream> 19 #include<algorithm> 20 #pragma comment(linker, "/STACK:102400000,102400000") 21 using namespace std; 22 #define A first 23 #define B second 24 const int mod=1000000007; 25 const int MOD1=1000000007; 26 const int MOD2=1000000009; 27 const double EPS=0.00000001; 28 //typedef long long ll; 29 typedef __int64 ll; 30 const ll MOD=1000000007; 31 const int INF=1000000010; 32 const ll MAX=1ll<<55; 33 const double eps=1e-5; 34 const double inf=~0u>>1; 35 const double pi=acos(-1.0); 36 typedef double db; 37 typedef unsigned int uint; 38 typedef unsigned long long ull; 39 int t; 40 char str[10]; 41 map<int,int> mp; 42 int l,r; 43 int l0,r0; 44 int exm; 45 int flag=0; 46 int n; 47 set<int>se; 48 set<int>::iterator it; 49 int biao=0; 50 int main() 51 { 52 scanf("%d",&t); 53 { 54 for(int k=1; k<=t; k++) 55 { 56 se.clear(); 57 mp.clear(); 58 biao=0; 59 scanf("%d",&n); 60 l=r=0; 61 printf("Case #%d: ",k); 62 for(int i=1; i<=n; i++) 63 { 64 scanf("%s",str); 65 if(strcmp(str,"PUSH")==0) 66 { 67 scanf("%d",&exm); 68 if(exm==0) 69 se.insert(l); 70 71 mp[l]=exm; 72 if(biao==0) 73 l++; 74 else 75 l--; 76 } 77 if(strcmp(str,"POP")==0) 78 { 79 if(biao==0) 80 l--; 81 else 82 l++; 83 } 84 if(strcmp(str,"REVERSE")==0) 85 { 86 if(biao==0) 87 { 88 l--; 89 r--; 90 swap(l,r); 91 biao=1; 92 } 93 else 94 { 95 l++; 96 r++; 97 swap(l,r); 98 biao=0; 99 } 100 } 101 if(strcmp(str,"QUERY")==0) 102 { 103 if(l==r) 104 printf("Invalid. "); 105 else 106 { 107 if(biao==0) 108 { 109 int exm; 110 int gg=0; 111 for(it=se.begin(); it!=se.end(); it++) 112 { 113 if(*it>=r) 114 { 115 exm=*it; 116 gg=1; 117 break; 118 } 119 } 120 if(exm>=l)/*评论区hack点 更正 */ 121 gg=0; 122 if(gg==0) 123 { 124 if((l-r)%2==0) 125 printf("0 "); 126 else 127 printf("1 "); 128 } 129 else 130 { 131 if(l==r+1) 132 printf("%d ",mp[exm]); 133 else 134 { 135 if(exm==(l-1)) 136 exm--; 137 if((exm-r+1)%2) 138 printf("1 "); 139 else 140 printf("0 "); 141 } 142 } 143 } 144 else 145 { 146 int exm; 147 int gg=0; 148 if(se.size()!=0) 149 { 150 it=--se.end(); 151 for(;; it--) 152 { 153 if(*it<=r) 154 { 155 exm=*it; 156 gg=1; 157 break; 158 } 159 if(it==se.begin()) 160 break; 161 } 162 } 163 if(exm<=l)/*评论区hack点 更正*/ 164 gg=0; 165 if(gg==0) 166 { 167 if((r-l)%2==0) 168 printf("0 "); 169 else 170 printf("1 "); 171 } 172 else 173 { 174 int hhh=1; 175 if(l==r-1) 176 printf("%d ",mp[exm]); 177 else 178 { 179 if(exm==(l+1)) 180 exm++; 181 if((r-exm+1)%2) 182 printf("1 "); 183 else 184 printf("0 "); 185 } 186 } 187 } 188 } 189 } 190 } 191 } 192 } 193 return 0; 194 } 195 /* 196 2 197 6 198 PUSH 1 199 PUSH 1 200 PUSH 1 201 PUSH 1 202 REVERSE 203 QUERY 204 5 205 PUSH 1 206 PUSH 1 207 PUSH 1 208 PUSH 1 209 QUERY 210 */