Hdu 5929 Basic Data Structure(双向数组+规律)

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.

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

解题思路:这题。。。一直超时一直爽,,,关键在两个地方,一个是反转,一个是找0;先说说反转,这里我们采用的是双向数组,从中间开始存,若反转对另一头操作即可,即两头操作;再说说找0,这题有个规律,仔细一看就可以发现,任何数在遇见0时的结果都是为1,那么再对答案进行输出时(计算是从top到bottom),就有一个简单的处理方法,若为正向,则只需找到最左端的0,即可计算出结果(在该0的右端全部可以忽略,因为无论计算出是什么,在遇到这个0的时候都会变成1,在0前1的个数,还需加上本身,若为奇数则为1,若为偶数则为0);若为反向,则同理,换一边操作即可!!!

代码如下:

#include <stdio.h>
#include <string.h>
const int N = 2e5;
int a[2*N+10];                          //双向数组
int b[2*N+10];                          //用于存储0的位置
int main()
{
    int T,n;
    int count = 1;
    scanf("%d",&T);
    while(T--)
    {
        int left = N ,right = N+1;      //left表示左端,right表示右端
        int l = N , r = N+1;            //l表示左端0的位置,r表示右端0的位置
        int len = 0 , le = 0;           //len表示元素个数,le表示0的个数
        int flag = 1;
        printf("Case #%d:\n",count++);
        scanf("%d",&n);
        while(n--)
        {
            char str[20];
            scanf("%s",str);
            if(strcmp(str,"PUSH") == 0) //入
            {
                int x;
                scanf("%d",&x);
                if(flag == 1)           //正向
                {
                    a[right] = x;
                    if(x == 0)          //若为0,则需记录位置
                    {
                        b[r++] = right; //记录0的位置
                        le++;           //0的个数增加
                    }
                    right++;
                }
                else                    //反向
                {
                    a[left] = x;        //操作同上,只是另一个方向的操作
                    if(x == 0)
                    {
                        b[l--] = left;
                        le++;
                    }
                    left--;
                }
                len++;
            }
            else if(strcmp(str,"POP") == 0) //出
            {
                int temp;
                if(flag == 1)
                {
                    temp = a[right-1];
                    right--;
                    if(temp == 0)           //若0出,则标记的也应出
                    {
                        r--;
                        le--;
                    }
                }
                else
                {
                    temp = a[left+1];
                    left++;
                    if(temp == 0)
                    {
                        l++;
                        le--;
                    }
                }
                len--;
            }
            else if(strcmp(str,"REVERSE") == 0) //反
                flag *= -1;
            else                                //answer
            {
                if(len <= 0)  printf("Invalid.\n");         //数组中没有元素
                else if(le <= 0)   printf("%d\n",len%2);    //数组中没有0
                else if(flag == 1)                          //正向
                {
                    int t = b[l+1];                         //最左端0的位置
                    if(t == right -1)                       //若在最右端出现
                        printf("%d\n",(t-left+1)%2);
                    else
                        printf("%d\n",(t-left)%2);
                }
                else                                        //反向
                {
                    int t = b[r-1];                         //最右端0的位置
                    if(t == left+1)                         //若在最左端出现
                        printf("%d\n",(right-t+1)%2);
                    else
                        printf("%d\n",(right-t)%2);
                }
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值