(HDU 5929)Basic Data Structure 双端队列+模拟 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >

原创 2017年01月03日 20:11:59

Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1102 Accepted Submission(s): 290
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 | We have carefully selected several similar problems for you: 6010 6009 6008 6007 6006

题意:
有一个栈,有四个操作:
push x : 将x插入栈顶(x=0 || x=1)
pop : 删除栈顶元素
reverse: 将栈所以元素反转
query:从栈顶到栈底所有数的nand值
让你输出每次查询的结果,若为空则输出Invalid.

分析:
关于push和pop我们可以很简单的模拟出来。但是若我们每次还模拟reverse和计算query那么就会超时。所以我们不能完全进行模拟
关于nand计算我们可以发现,只要有0参加,那么结果就是1
所以当我们知道了离栈底最近的一个0的位置后,若该位置的右边还有数,那么不管是什么样的,最后和0计算结果一定是1,若没有数了,那么结果就是0,然后就可以根据0前面的连续1的个数可以 很简单的计算出答案。
所以说我们要记录距离栈底最近的0的位置,但是由于还有反转的情况,所以我们还要记录离栈底最远的0的位置,由于有pop操作,所以我们应该记录所有的0的位置。
我们用flag来表示反转的情况,由此来判断是对栈顶操作还是对栈底操作。由于两边都要删除和添加数,所有我们用双端队列来完成上述的所有操作。
剩余的就是进行模拟就可以了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <deque>
#include <algorithm>
using namespace std;

const int maxn = 400010;
int a[maxn];
int l,r,n,flag;
deque<int> dq;

void init()
{
    flag = 1;
    l = n;
    r = n-1;
    while(!dq.empty()) dq.pop_back();
}

void PUSH(int x)
{
    if(flag)
    {
        a[++r] = x;
        if(x == 0) dq.push_back(r);
    }
    else
    {
        a[--l] = x;
        if(x == 0) dq.push_front(l);
    }
}

void POP()
{
    if(flag)
    {
        if(a[r] == 0) dq.pop_back();
        r--;
    }
    else
    {
        if(a[l] == 0) dq.pop_front();
        l++;
    }
}

void REV()
{
    flag ^= 1;
}

void query()
{
    if(dq.empty()) //没有0
    {
        if(r < l) //为空
        {
            printf("Invalid.\n");
            return;
        }
        int num = r - l + 1;
        if(num&1) printf("1\n");
        else printf("0\n");
    }
    else
    {
        if(flag) // 没有翻转
        {
            int fr = dq.front();
            int num = fr - l;
            if(num&1)
            {
                if(fr == r) printf("1\n"); //0为最后一个
                else printf("0\n");//0后面还有1
            }
            else
            {
                if(fr == r) printf("0\n"); //0为最后一个
                else printf("1\n");//0后面还有1
            }
        }
        else
        {
            int fr=dq.back();
            int num=r-fr;
            if(num&1)
            {
                if(fr==l)printf("1\n");
                else printf("0\n");
            }
            else
            {
                if(fr==l)printf("0\n");
                else printf("1\n");
            }
        }
    }
    return;
}

int main()
{
    int t,kase=1;
    char s[10];
    scanf("%d",&t);
    while(t--)
    {
        printf("Case #%d:\n",kase++);
        scanf("%d",&n);
        init();
        while(n--)
        {
            scanf("%s",s);
            if(s[0] == 'P')
            {
                if(s[1] == 'U')
                {
                    int x;
                    scanf("%d",&x);
                    PUSH(x);
                }
                else POP();
            }
            else if(s[0] == 'R') REV();
            else query();
        }
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

hdu 5929 Basic Data Structure(找规律,模拟)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意: 维护一个栈,支持往栈里塞 0/1 ,弹栈顶,翻转栈,询问从栈顶到栈底按顺序 NAND ...

hdu 5929 Basic Data Structure 双端队列

Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot...

hdu 5929 Basic Data Structure 2016CCPC东北地区大学生程序设计竞赛

http://acm.hdu.edu.cn/showproblem.php?pid=5929题目大意:模拟一个栈,有四种操作: PUSH x:往栈里压入x POP:丢掉栈顶元素 REVERSE:...

HDU5929-Basic Data Structure

Basic Data Structure                                                                            Tim...

hdu5929Basic Data Structure

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:要求模拟一个存入0,1的栈,有4个操作1:push a,在栈顶插入a。2:pop,删除栈顶。...
  • Fsss_7
  • Fsss_7
  • 2016年10月06日 21:02
  • 485

双端队列+与非运算——Basic Data Structure ( HDU 5929 )

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5929 分析: 实现一个数据结构,能够满足如下四种操作: PUSH:把一个元素( 0 or 1 ...
  • FeBr2
  • FeBr2
  • 2016年10月07日 11:07
  • 417

HDU5929 Basic Data Structure(双端队列)

Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot...

HDU 5929 Basic Data Structure (模拟)

参考博客 根据nand的运算法则,发现 任何 nand 0 都等于1,题目是要从top nand 到 bottom,所以如果没有reverse的话就是要记录第一个出现的0的位置,然后看在这个0之前有...

[HDU 5929] Basic Data Structure (脑洞题)

链接HDU 5929题意CCPC东北四省的1008。需要实现一个双端栈结构,可以从两端push和pop,存的元素只有1/0两种,并且需要支持查询左端到右端或右端到左端元素依次nand的值。题解感觉就是...

map及其相关函数的用法

C++ Maps are sorted associative containers that contain unique key/value pairs. For example, you cou...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:(HDU 5929)Basic Data Structure 双端队列+模拟 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
举报原因:
原因补充:

(最多只允许输入30个字)