HDU-5929 Basic Data Structure

Basic Data Structure

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1174    Accepted Submission(s): 300

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

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.

Source

2016CCPC东北地区大学生程序设计竞赛 - 重现赛

Recommend

wange2014

————————————随便说说——————————————

一道模拟赛的题目,啊……爆零模拟赛。

怎么说呢,正式比赛的题目强度很高啊……

首先作为签到题的A和C的难度就略奇葩,因为难点在题面上。

论A+B和打印输入数字如何出的高端大气上档次呵呵呵呵呵。

然后就做了一道连连看的题目就逃了。

不如直接赛后补题呵呵呵

顺便说一下这次比赛真是膜的飞起。

————————————正文————————————————

 

  这道题有最大20次测试,每次有200000次操作,那么对于4个操作来看一下啊,push和pop很明显没有问题,和操作次数无关,但是reverse和query就有问题了,reverse的问题在于无法直接进行翻转,因为直接进行翻转操作消耗O(n)时间,但是如果之前100000次操作全是push然后后面100000次操作全是reverse,那么用到的时间很明显会暴表,所以不能直接翻转。类似,query就不能用遍历的方法算出。

  先解决翻转问题,我们可以意识到,从左往右,这只不过是人类的习惯,所以我们当提到反转的时候会下意识的选择把对象从右往左重新排一遍,然后我们在用以往习惯的方式从左往右读的时候就会读到原来应该从右往左读才能读到的内容。

  但是计算机只需要从右往左扫就好了,立一个flag,这个flag将决定现在是从右往左,还是从左往右。同样的,pop和push操作将因为这个flag来决定他们该干的事情。实现这个结构的一个比较方便的工具是deque

  那么剩下的就是一个QUERY的缩减时间。

  通过观察真值表,我们可以观察到,在计算式中,只要某一位是零,不管其前面的计算结果是0还是1,其结果必然是1,这个就可以成为一个突破口。

  那么对于任意一个计算式,只需找到它最后一个0的位置,然后接下来就看剩下的1的个数,而我们也可以发现若1是奇数个,其结果为1,1是偶数个,其结果为0.(此时计算的是0转化成的1个1 加上后面存在的1的个数 的和)

  同时再考虑一个特殊情况,就是当最后一个0前面没有任何数字,那么其值就单纯看其后有多少个1,而不需要考虑该0和前面的数字计算生成的1.

  为了不增加额外的查找时间,采用deque记录两边0的位置,那么query时只要用对应方向的目录值减去对应0的位置,加上上述的特殊讨论,即可得到答案。

——————————————code——————————————

 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<climits>
#include<map>
#include<deque>
#define MAX 3000
#define math 1000
#define ll long long
using namespace std;
int a[6000005];
int main(void)
{
    int num;
    cin >> num;
    for (int i = 0; i < num; i++)
    {
        vector<int>S;
        deque<int>sto;
        int lef = 300000 - 1, rig = 300000, flag = 1, x, tim;
        scanf("%d", &tim);
        printf("Case #%d:\n", i + 1);
        for (int i = 0; i < tim; i++)
        {
            char s_[20];
            string s;
            scanf("%s", s_);
            s = s_;
            if (s == "PUSH")
            {
                scanf("%d", &x);
                if (flag)
                {
                    a[rig] = x;
                    if (x == 0)
                        sto.push_back(rig);
                    rig++;
                }
                else
                {
                    a[lef] = x;
                    if (x == 0)
                        sto.push_front(lef);
                    lef--;
                }
            }
            if (s == "POP")
            {
                if (flag)
                {
                    rig--;
                    if (a[rig] == 0)
                    {
                        sto.pop_back();
                    }
                }
                else
                {
                    lef++;
                    if (a[lef] == 0)
                        sto.pop_front();
                }
            }
            if (s == "REVERSE")
            {
                flag ^= 1;
            }
            if (s == "QUERY")
            {
                if (lef == rig - 1)  printf("Invalid.\n");
                else if (sto.empty()) printf("%d\n", (rig - lef - 1) % 2);
                else
                {
                    if (flag)
                    {
                        printf("%d\n", (sto.front() - lef - 1 + (sto.front() != rig - 1)) % 2);
                    }
                    else
                    {
                        printf("%d\n", (rig - sto.back() - 1 + (sto.back() != lef + 1)) % 2);
                    }
                }
            }


        }
    }
}

 

posted @ 2017-02-09 13:48  duskcloudxu  阅读(707)  评论(0编辑  收藏  举报