HDU - 5929 Basic Data Structure

Basic Data Structure - HDU 5929 - Virtual Judge (vjudge.net)

 

Basic Data Structure - HDU 5929 - Virtual Judge (vjudge.net)

 

#include<iostream>
#include<cstdio>
#include<stdio.h>
using namespace std;
const int N=400080;
int st[N];
char op[10];

int main()
{
    int t,x;
    int cnt;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int l=200001,r=200000;//l设为r+1,则st[l]即为第一个出现的0的位置
        int dir=1;
        int head=0,tail=1;
        printf("Case #%d:\n",cnt++);
        while(n--)
        {
            scanf("%s",op);
            if(op[2]=='S')
            {
                scanf("%d",&x);
                if(dir)//这代表一侧
                {
                    head++;
                    if(!x)//如果输入为0,记录位置
                        st[++r]=head;//记录各个0出现的位置
                }
                else//代表另一侧
                {
                    tail--;
                    if(!x)//如果输入为0,记录位置
                        st[--l]=tail;
                }
            }
            else if(op[2]=='P')
            {
                if(dir)
                {
                    head--;
                    if(st[r]>head)//如果那个位置有0则去掉就好了
                        r--;
                }
                else
                {
                    tail++;
                    if(st[l]<tail)//如果有0同样去掉
                        l--;
                }
            }
            else if(op[2]=='V')
            {
                dir^=1;//逆转只需要改变nand开始的方向
            }
            else
            {
                if(head<tail) puts("Invalid.");////初始即tail-head=1的
                else if(l>r) printf("%d\n",(head-tail+1)&1); //全是1的情况
                else
                {
                    if(dir)
                    {
                        int tmp=st[l]-tail;//0之后的1的个数
                        if(st[l]==head) //如果0之前没有东西,那之前的答案是0
                            tmp++;
                        printf("%d\n",!(tmp&1));
                    }
                    else
                   {
                        int tmp = head - st[r];
                        if(tail==st[r]) tmp++; //如果0之前没有东西,那之前的答案是0
                        printf("%d\n",!(tmp&1));
                   }
                }
            }

        }
    }
    return 0;
}
 

 

posted @ 2021-05-02 21:52  BlackSnow  阅读(43)  评论(0)    收藏  举报