HDU 5929Basic Data Structure双向队列

xiaoxiao2021-03-01  18

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,⋯,a1atop,atop−1,⋯,a1 is corresponding to the element of the Stack from top to the bottom, value=atopvalue=atop nand atop−1atop−1 nand ... nand a1a1. 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≤20T≤20), which indicates the number of test cases.  For each test case, the first line contains only one integers N (2≤N≤2000002≤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,因为除单独一个0的情况,其他运算只要碰到0运算结果就是1。且这个栈可以翻转,所以我们要记录所有0的位置。

每次算出离栈底最近的0下1的个数。

#include<bits/stdc++.h> #define maxn 400010 typedef unsigned long long ll; using namespace std; int main() { int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++) { int n; scanf("%d",&n); int a[maxn];//存放0的位置 int l=maxn/2,r=maxn/2-1,z=maxn/2,y=maxn/2-1; int p=1; memset(a,-1,sizeof(a)); printf("Case #%d:\n",cas); for(int i=0;i<n;i++) { char s[10]; scanf("%s",s); if(strcmp(s,"PUSH")==0) { int d; scanf("%d",&d); if(p==1) { if(d==0) a[++r]=++y; else ++y; } else { if(!d) a[--l]=--z; else --z; } } else if(strcmp(s,"POP")==0) { if(p==1) { if(y>=z) { if(a[r]!=-1&&a[r]==y)//若栈顶是0 r--; y--; } } else { if(z<=y) { if(a[l]!=-1&&a[l]==z) l++; z++; } } } else if(strcmp(s,"REVERSE")==0) { p*=-1; } else if(strcmp(s,"QUERY")==0) { if(y<z)//栈为空 printf("Invalid.\n"); else if(r<l)//若没有0 { if((y-z+1)%2) puts("1"); else puts("0"); } else if(p==1) { if(a[l]==y)//若栈顶为0 { if((y-z)%2) puts("1"); else puts("0"); } else { if((a[l]-z)%2) puts("0"); else puts("1"); } } else { if(a[r]==z)//若栈顶为0 { if((y-z)%2) puts("1"); else puts("0"); } else { if((y-a[r])%2) puts("0"); else puts("1"); } } } } } return 0; }

 

转载请注明原文地址: https://www.6miu.com/read-3450011.html

最新回复(0)