H - 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 = 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

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所求的值为从栈顶到栈底的与非值。
思路: PUSH POP REVERSE操作完全可以用数组模拟,但是关键就在于怎么计算栈的值。注意与非操作 1 0 = 1 0 1 = 1 0 0 = 1。也就是说任何数遇到0都会变成1,换句话说,
0挡住了前面的所有数 ,那么我们只需要记录栈底最后一个0即可,再结合1的数目(1为奇数则为1,1为偶数则为0)。

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<queue>
#include<map>

using namespace std;
typedef long long ll;

const int maxn = 4e5 + 7;
int Q[maxn];

int main()
{
    int T;scanf("%d",&T);
    int kase = 0;
    while(T--)
    {
        int n;scanf("%d",&n);
        int dir = 1;
        int en = 1e5 + 7,sta = 1e5 + 7;
        int zero_right = -1,zero_left = 1e6;
        printf("Case #%d:\n",++kase);
        for(int i = 1;i <= n;i++)
        {
            char str[100] = {0};scanf("%s",str);
            if(str[0] == 'P' && str[1] == 'U')
            {
                int num;scanf("%d",&num);
                if(dir == 1)
                {
                    Q[++en] = num;
                    if(num == 0)
                    {
                        zero_right = en;
                        if(zero_left == 1e6)
                            zero_left = en;
                    }
                }
                else if(dir == 0)
                {
                    Q[sta--] = num;
                    if(num == 0)
                    {
                        zero_left = sta + 1;//or en2 +- 1
                        if(zero_right == -1)
                            zero_right = sta + 1;
                    }
                }
            }
            else if(str[0] == 'P' && str[1] == 'O')
            {
                int num;scanf("%d",&num);
                if(dir == 1)
                {
                    en--;
                    
                }
                else if(dir == 0)
                {
                    sta++;
                }
            }
            else if(str[0] == 'R')
            {
                dir ^= 1;
            }
            else if(str[0] == 'Q')
            {
                if(sta >= zero_left)
                {
                    zero_left = 1e6;
                    for(int j = sta + 1;j <= en;j++)
                    {
                        if(Q[j] == 0)
                        {
                            zero_left = j;
                            break;
                        }
                    }
                }
                if(en < zero_right)
                {
                    zero_right = -1;
                    for(int j = en;j > sta;j--)
                    {
                        if(Q[j] == 0)
                        {
                            zero_right = j;
                            break;
                        }
                    }
                }
                if(en == sta)
                {
                    printf("Invalid.\n");
                }
                else if(en - sta == 1)
                {
                    printf("%d\n",Q[en]);
                }
                else if(zero_left == 1e6 && zero_right == -1)//全是1
                {
                    printf("%d\n",(en - sta) % 2);
                }
                else if(zero_left == sta + 1 && zero_right == en)//全是0
                {
                    printf("1\n");
                }
                else if(en - sta == 2)
                {
                    if(Q[en] == 1 && Q[en - 1] == 1)
                        printf("0\n");
                    else
                        printf("1\n");
                }
                else if(dir == 1)
                {//1左边没有0  2左边只有一个0 3左边除了一个0外还有其他数字 4左边全是1
                    if(zero_left == sta + 1)
                        printf("1\n");
                    else if(zero_left == en)
                        printf("%d\n",(zero_left - sta - 1) % 2);
                    else
                        printf("%d\n",(zero_left - sta) % 2);
                }
                else if(dir == 0)
                {
                    if(zero_right == en)
                        printf("1\n");
                    else if(zero_right == sta + 1)
                        printf("%d\n",(en - zero_right) % 2);
                    else
                        printf("%d\n",(en - zero_right + 1) % 2);//0的坐标是zero_left,
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值