Basic Data Structure

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

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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5932 5931 5930 5928 5927 
/*
双向队列,记录从开头开始到第一个0的位置的1有多少个,因为0与任何nand都是1 比赛的时候竟然想不起来双向队列.......愣是用一个数组加了两个指针模拟了一个双向队列。
*/
#include<bits/stdc++.h>
#define N 500000
using namespace std;
int s[N];
deque<int >q;//用来存放所有0的位置
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
int t,n;
char op[];
scanf("%d",&t);
int Case=;
while(t--)
{
memset(s,-,sizeof s);
int f=;
scanf("%d",&n);
int r=;
int l=r-;
int fa=;///记录栈里面的总数
q.clear();
printf("Case #%d:\n",Case++);
while(n--)
{
scanf("%s",op);
int a;
if(op[]=='P'&&op[]=='U')
{
scanf("%d",&a);
if(f)
{
s[r]=a;
if(!a)
q.push_back(r);
r++;
}
else
{
s[l]=a;
if(!a)
q.push_front(l);
l--;
}
fa++;
}
else if(op[]=='P'&&op[]=='O')
{
if(!fa)
continue;
if(f)
{
if(s[r-]==)
q.pop_back();
r--;
}
else
{
if(s[l+]==)
q.pop_front();
l++;
}
fa--;
}
else if(op[]=='Q')
{
//cout<<"cur="<<cur<<endl;
int cur=;
if(fa==)
{
cout<<"Invalid."<<endl;
}
else if(fa==)
{
cout<<s[l+]<<endl;
}
else
{
if(f)
{
if(q.empty())
cur=fa;
else
{
cur=q.front()==r-?fa-:q.front()-l;
}
}
else
{
if(q.empty())
cur=fa;
else
{
cur=q.back()==l+?fa-:r-q.back();
} }
if(cur%==)
cout<<""<<endl;
else
cout<<""<<endl;
}
}
else
{
f^=;
}
}
}
return ;
}

Basic Data Structure的更多相关文章

  1. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. HDU 5929 Basic Data Structure 模拟

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

  3. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

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

  4. Basic Data Structure HDU - 5929 (这个模拟我要报警了)

    Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operati ...

  5. hdu 5929 Basic Data Structure

    ゲート 分析: 这题看出来的地方就是这个是左结合的,不适用结合律,交换律. 所以想每次维护答案就不怎么可能了.比赛的时候一开始看成了异或,重读一遍题目了以后就一直去想了怎么维护答案...... 但是很 ...

  6. HDU 5929 Basic Data Structure(模拟 + 乱搞)题解

    题意:给定一种二进制操作nand,为 0 nand 0 = 10 nand 1 = 1 1 nand 0 = 1 1 nand 1 = 0 现在要你模拟一个队列,实现PUSH x 往队头塞入x,POP ...

  7. 【推导】【线段树】hdu5929 Basic Data Structure

    题意: 维护一个栈,支持以下操作: 从当前栈顶加入一个0或者1: 从当前栈顶弹掉一个数: 将栈顶指针和栈底指针交换: 询问a[top] nand a[top-1] nand ... nand a[bo ...

  8. Finger Trees: A Simple General-purpose Data Structure

    http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...

  9. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. 走进AngularJS

      前  言 xiaoq AngularJS 通过新的属性和表达式扩展了 HTML. 使用起来非常方便. 1. AngularJS的指令与表达式 AngularJS 通过 指令 扩展了 HTML,且通 ...

  2. 王者荣耀_KEY

    WZRY 为了排位赛的Cjj神,最近耗尽气力来打WZRY. Cjj神最近有N局预约的排位赛,其中第i局需要耗时Li的时间.因为浓浓的Gay情,Cjj神不能改变这些排位赛的的顺序.作为一个很有(mei) ...

  3. 封装好的图片滑动框架(AndroidImageSlider)

    前言 广告轮播条的重要性不言而喻.在很多类型app中出场率都很高. 今天给大家介绍一个轮播图开源项目,这个项目把轮播图需要的ViewPager跟计时器做了封装,使用极其方便,支持gradle在线依赖. ...

  4. SpringMVC——使用RequestDispatcher.include()和HttpServletResponseWrapper动态获取jsp输出内容

    介绍本篇内容前,先抛出我遇到的问题或者说是需求!(精读阅读本篇可能花费您15分钟,略读需5分钟左右) 一:需求说明 有一个Controller有两个方法 第一个方法通过指定的路径和参数去渲染jsp内容 ...

  5. Query DSL(2)----Full text queries

    Match Query match查询接受文本/数值/日期 { "match" : { "message" : "this is a test&quo ...

  6. 『诡异的』VL10B创建外向交货单出错解决全过程

    一直觉得SAP STO的业务模式配置起来还是挺简单的,无非就是关联一下采购单与交货单的关系,以及相应工厂的装运数据,其他像主数据的设置也没有什么特别的.相比ICS模式,它少了IDOC的配置,所以还是很 ...

  7. 最接近原生APP体验的高性能前端框架-MUI

      前  言 轻量,原生UI,流畅体验,是MUI的三个特征.   1. 新手指南 快速体验 1. 下载Hello mui App 下载已打包好的Hello mui 手机app,直接在手机上体验mui的 ...

  8. Conscription poj3723(最大生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6870   Accepted: 2361 Desc ...

  9. Android 性能测试之方向与框架篇

    假期结束,你的状态有没有回归?那么,放空脑袋后,先来学习学习,欢迎大家继续关注腾讯云技术社区. 作者:李帅 导语 借项目的开发周期,把思考了一段时间的场景化性能测试框架搭建起来,包括 耗电性能测试.内 ...

  10. Entity Framework相关介绍

    在深入学习某项技术之前,应该努力形成对此技术的总体印象,并了解其基本原理,本文的目的就在于此. 一.理解EF数据模型 EF本质上是一个ORM框架,它需要把对象映射到底层数据库中的表,为此,它使用了三个 ...