H - Basic Data Structure HDU - 5929

来源:互联网 发布:卡巴斯基网络防火墙 编辑:程序博客网 时间:2024/06/05 23:48

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,atop1,,a1atop,atop−1,⋯,a1 is corresponding to the element of the Stack from top to the bottom, value=atopvalue=atop nand atop1atop−1 nand ... nand a1a1. 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 (T20T≤20), which indicates the number of test cases. 

For each test case, the first line contains only one integers N (2N2000002≤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
28PUSH 1QUERYPUSH 0REVERSEQUERYPOPPOPQUERY3PUSH 0REVERSEQUERY
Sample Output
Case #1:11Invalid.Case #2:0

思路:双向队列存储0的位置。因为不管0前面的值为多少,碰到0就会变成1.然后看一下剩余的1的个数就可以了。

坑点:

1.双向队列首先要判空。直接算1的个数就可以了。

2.0前面没有值得情况。这时候0的1不计算。

#include <bits/stdc++.h>using namespace std;const int MAXN=4e5+7;const int inf=1e9;char s[10];int num[MAXN];int main(){    int i;    int t;    int n;    int l,r;    scanf("%d",&t);    for(int tt=1; tt<=t; ++tt)    {        deque<int>q;        scanf("%d",&n);        printf("Case #%d:\n",tt);        int x;        l=r=200001;        int flag=0;        for(i=0; i<n; ++i)        {            scanf("%s",s);            if(s[0]=='P')            {                if(s[1]=='U')                {                    scanf("%d",&x);                    if(!flag)                    {                        num[r++]=x;                        if(!x)q.push_back(r-1);                    }                    else                    {                        num[--l]=x;                        if(!x)q.push_front(l);                    }                }                else                {                    if(!flag)                    {                        r--;                        if(!num[r])q.pop_back();                    }                    else                    {                        if(!num[l])q.pop_front();                        l++;                    }                }            }            else if(s[0]=='R')flag=(flag+1)%2;            else if(s[0]=='Q')            {                if(l>=r)puts("Invalid.");                else if(q.empty())printf("%d\n",(r-l)%2);                else                {                    int ans;                    if(r-l==1)printf("%d\n",num[l]);                    else                    {                        if(!flag)                        {                            ans=(q.front()-l+(q.front()!=r-1))%2;                        }                        else                        {                            ans=(r-q.back()-1+(q.back()!=l))%2;                        }                        printf("%d\n",ans);                    }                }            }        }    }    return 0;}




0 0