Basic Data Structure HDU - 5929 双向数组

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 = 10 nand 1 = 11 nand 0 = 11 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.
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. 


画得真好!

我画的真好!

之前错在query没有考虑到0最后入队的时候
别人用双向队列做的,待学

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int T,n,a,bot,st[400005],f,top[2],p_0[400005],num_0[2];//top=top坐标,num_0=该方向存0的个数; 
char ord[10];
int main()
{
    scanf("%d",&T);
    for(int ca=1;ca<=T;ca++)
    {
        printf("Case #%d:\n",ca);
        f=1;
        top[0]=200000;top[1]=200001;
        num_0[0]=200000;num_0[1]=200001;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",ord);
            if(ord[2]=='S')
            {
                scanf("%d",&a);
                if(f)
                {
                    if(a==0)p_0[num_0[1]++]=top[1];
                    st[top[1]++]=a;
                }
                else
                {
                    if(a==0)p_0[num_0[0]--]=top[0];
                    st[top[0]--]=a;
                }
            }
            else if(ord[2]=='P')
            {
                if(f)
                {
                    if(st[--top[1]]==0)p_0[--num_0[1]]=-1;
                    st[top[1]]=-1;
                }
                else
                {
                    if(st[++top[0]]==0)p_0[++num_0[0]]=-1;
                    st[top[0]]=-1;
                }
            }
            else if(ord[2]=='V')
            {
                f=!f;
            }
            else if(ord[2]=='E')
            {
                if(top[1]-top[0]==1)printf("Invalid.\n");
                else if(top[1]-top[0]==2)printf("%d\n",st[top[0]+1]);
                else
                {
                    int res;
                    if(f)
                    {
                        if(num_0[1]-num_0[0]==1)res=top[1]-top[0];
                        else
                        {
                            res=p_0[num_0[0]+1]+1-top[0];
                            if(p_0[num_0[0]+1]==top[1]-1)res--;/0在最后时
                        }
                    }
                    else
                    {
                        if(num_0[1]-num_0[0]==1)res=top[1]-top[0];
                        else
                        {
                            res=top[1]-p_0[num_0[1]-1]-1;
                            if(p_0[num_0[1]-1]==top[0]+1)res++;0在最后时
                        }
                    }
                    printf("%d\n",res%2==0?1:0);
                }
            }
        }
        for(int i=num_0[0];i<=num_0[1];i++)p_0[i]=-1;
    }
    return 0;
}

**

wrong code for record thread

**

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int T,n,a,bot,st[400005],f,top[2],p_0[400005],num_0[2];//top=top坐标,num_0=该方向存0的个数; 
char ord[10];
int main()
{
    scanf("%d",&T);
    for(int ca=1;ca<=T;ca++)
    {
        printf("Case #%d:\n",ca);
        f=1;
        top[0]=200000;top[1]=200001;
        num_0[0]=200000;num_0[1]=200001;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",ord);
            if(ord[2]=='S')
            {
                scanf("%d",&a);
                if(f)
                {
                    if(a==0)p_0[num_0[1]++]=top[1];
                    st[top[1]++]=a;
                }
                else
                {
                    if(a==0)p_0[num_0[0]--]=top[0];
                    st[top[0]--]=a;
                }
            }
            else if(ord[2]=='P')
            {
                if(f)
                {
                    if(st[--top[1]]==0)p_0[--num_0[1]]=-1;
                    st[top[1]]=-1;
                }
                else
                {
                    if(st[++top[0]]==0)p_0[++num_0[0]]=-1;
                    st[top[0]]=-1;
                }
            }
            else if(ord[2]=='V')
            {
                f=!f;
            }
            else if(ord[2]=='E')
            {
                if(top[1]-top[0]==1)printf("Invalid.\n");
                else if(top[1]-top[0]==2)printf("%d\n",st[top[0]+1]);
                else
                {
                    int res;
                    if(f)
                    {
                        if(num_0[1]-num_0[0]==1)res=top[1]-top[0];
                        else
                        {
                            res=p_0[num_0[0]+1]+1-top[0];
                        }
                    }
                    else
                    {
                        if(num_0[1]-num_0[0]==1)res=top[1]-top[0];
                        else
                        {
                            res=top[1]-p_0[num_0[1]-1]-1;
                        }
                    }
                    printf("%d\n",res%2==0?1:0);
                }
            }
        }
        for(int i=num_0[0];i<=num_0[1];i++)p_0[i]=-1;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值