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): 207    Accepted Submission(s): 41

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 notchange 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

题意:

给一个栈.有push,pop,query ,reverse这些操作,对于每个询问输出这个栈从栈顶到底进行题目给的这个运算后的结果;

思路:

可以发现每次运算的结果跟到栈底最近的0下面有多少个1有关,所以双端队列里面维护的是0的位置,然后开一个2倍的数组模拟那个栈,每次翻转的时候就用flag 记录

是正向还是反向;然后就搞搞;我用数组模拟速度更快,而且比较和确定这个元素在当前情况下的相对位置也好确定一些;

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;
typedef unsigned long long ULL;
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar());
    for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + ‘0‘);
    putchar(‘\n‘);
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+4;
const int maxn=2e5+20;
const double eps=1e-12;

int n,m,a[2*maxn];
char s[20];
deque<int>qu;
int flag,l,r;
void POP()
{
    if(flag)
    {
        if(a[r]==0)qu.pop_back();
        r--;
    }
    else
    {
        if(a[l]==0)qu.pop_front();
        l++;
    }
}
void PUSH(int x)
{
    if(flag)
    {
        a[++r]=x;
        if(x==0)qu.push_back(r);
    }
    else
    {
        a[--l]=x;
        if(x==0)qu.push_front(l);
    }
}
void Rev(){flag^=1;}
void query()
{
    if(qu.empty())
    {
        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=qu.front();
            int num=fr-l;
            if(num&1)
            {
                if(fr==r)printf("1\n");
                else printf("0\n");
            }
            else
            {
                if(fr==r)printf("0\n");
                else printf("1\n");
            }
        }
        else
        {
            int fr=qu.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 ;
}
inline void Init()
{
    flag=1;l=N;r=N-1;
    while(!qu.empty())qu.pop_back();
}
int main()
{
    int t,Case=0;
    read(t);
    while(t--)
    {
        Init();
        printf("Case #%d:\n",++Case);
        read(n);
        for(int i=1;i<=n;i++)
        {
            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;
}

  

时间: 2024-12-23 20:19:07

hdu-5929 Basic Data Structure(双端队列+模拟)的相关文章

HDU 5929 Basic Data Structure

题目:Basic Data Structure 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:t个测试数据,每个数据给出一个m表示操作数量,操作分为4种:PUSH x(x:0或1),POP,REVERSE(翻转栈里面的数),QUERY(假使栈内的数一个个出栈,并按出栈顺序将他们与非起来,问最终结果(注意,并不是真的出栈)) 思路: 做题的时候忘记了与非是按出栈顺序来的,一直按栈底到栈顶的顺序来,WA到死... 根据与非的性质,1.0与非

HDU 5929 Basic Data Structure 模拟

Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack: ? PUSH x: p

【SGU】271. Book Pile(双端队列模拟)

一摞书,2个操作,一个操作是在书堆上加一本,第二个将前K个书翻转 看别人用Splay树做的,但是可以用双端队列模拟,因为K个书之后的书位置已经定下来了,所以只需要记录在队列头加书还是尾加书 #include<cstdio> #include<string> #include<algorithm> #include<queue> #include<stack> #include<cstring> #include<iostream

HDU 4286 Data Handler (双端队列)

Data Handler Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2455    Accepted Submission(s): 616 Problem Description You are in charge of data in a company, so you are called "Data Handler&qu

UVa 210 Concurrency Simulator (双端队列+模拟)

题意:给定n个程序,每种程序有五种操作,分别为 var = constant(赋值),print var (打印), lock, unlock,end. 变量用小写字母表示,初始化为0,为程序所公有(一个程序里对某个变量修改可以会影响其他程序里的这个变量), 常数小于100(也就是说最多两位数). 每个时刻都只能有一个程序处于运行状态,其他的都在等待,上述五种操作用时分别是t1, t2, t3, t4, t5.运行中的程序, 每次最多能运行q个时间,当q个时间被用完后,它会被放在等待队列的尾部,

[HDOJ5929]Basic Data Structure(双向队列,规律)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:维护一个栈,支持往栈里塞 0/1 ,弹栈顶,翻转栈,询问从栈底到栈顶按顺序 NAND 的值. 题解:只要知道最后的 00 后面 11 的个数的奇偶性就行.可以用链表把所有 00 的位置存下来. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 6 const int maxn =

lintcode 二叉树的锯齿形层次遍历 (双端队列)

题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 返回其锯齿形的层次遍历为: [ [3], [20,9], [15,7] ] 思路: 我们用双端队列模拟一下这个过程

设计循环队列——写起来最清爽的还使用原生的deque 双端队列

622. 设计循环队列 难度中等89收藏分享切换为英文关注反馈 设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为“环形缓冲器”. 循环队列的一个好处是我们可以利用这个队列之前用过的空间.在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间.但是使用循环队列,我们能使用这些空间去存储新的值. 你的实现应该支持如下操作: MyCircularQueue(k): 构造器,设置队

队列的应用:双端队列

双端队列(Deque:double ended queue)就是一个两端都是结尾的队列.队列的每一端都可以插入数据项和移除数据项.相对于普通队列,双端队列的入队和出队操作在两端都可进行. 双端队列的示意图: left:左端    right:右端 这里我们使用最常用的顺序结构来存储双端队列,为了节省空间,把它首尾相连,构成循环队列.并且规定left指向左端的第一个元素,right指向右端的下一个位置.那么队空的判断则是left==right,队满是(left-1+MAX)%MAX==right或