题目链接:

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;
}

  

 

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

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

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

  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(模拟 + 乱搞)题解

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

  4. hdu 5929 Basic Data Structure

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

  5. HDU 4286 Data Handler --双端队列

    题意:有一串数字,两个指针,然后一些添加,删除,反转,以及移动操作,最后输出序列. 解法:可以splay做,但是其实双端队列更简便. 维护三个双端队列LE,MI,RI分别表示[L,R]序列左边,[L, ...

  6. HDU - 6386 Age of Moyu (双端队列+bfs)

    题目链接 双端队列跑边,颜色相同的边之间的花费为0,放进队首:不同的花费为1,放进队尾. 用Dijkstra+常数优化也能过 #include<bits/stdc++.h> using n ...

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

    题意:给定n个程序,每种程序有五种操作,分别为 var = constant(赋值),print var (打印), lock, unlock,end. 变量用小写字母表示,初始化为0,为程序所公有( ...

  8. bzoj 2457 [BeiJing2011]双端队列 模拟+贪心

    [BeiJing2011]双端队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 457  Solved: 203[Submit][Status][D ...

  9. HDU 6319 Ascending Rating (单调双端队列)

    题意:给定一个序列a[1..n],对于每个长度为m的连续子区间,求出区间的最大值和从左往右扫描该区间最大值的变化次数. 分析:先O(n)处理出整个序列的值.求出每个长度为m的连续区间中的最大值可以用单 ...

随机推荐

  1. ahjesus动态生成表达式树

    直接上方法,看的懂的拿去用,看不懂的找资料看懂 , , Double floorprice = , Double topprice = , string brandstr = "" ...

  2. ahjesus sql2005+游标示例

    DECLARE @TypeId INT, @Price1 FLOAT, @Original FLOAT DECLARE my_cursor CURSOR SCROLL FOR SELECT TypeI ...

  3. 移除NDK方法

    以下内容由:于伟建 提供 删除.project中的以下字段基本都是有cdt关键字的位置 删除.cproject然后重启eclipse,clean,重新编译我这里还有错误,就手动删了gen 删除包含cd ...

  4. 【GOF23设计模式】组合模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_组合模式.树状结构.杀毒软件架构.JUnite底层架构.常见开发场景 package com.test.composite ...

  5. jQuery对复选框(checkbox)的全选,全不选,反选等的操作

    效果截图: HTML代码: <body><ul id="list"> <li><label><input type=" ...

  6. CKEditor与CKFinder的配置

    CKEditor与CKFinder的配置使用(一) 将CKEditor 与 CKFinder 的包含在项目中 从http://cksource.com网站上下载CKEditor与CKFinder,并将 ...

  7. CSS 属性 - 伪类和伪元素的区别

    伪类和伪元素皆独立于文档结构.它们获取元素的途径也不是基于id.class.属性这些基础的元素特征,而是在处于特殊状态的元素(伪类),或者是元素中特别的内容(伪元素).区别总结如下: ①写法不一样: ...

  8. SAP ALV中同一列的不同行显示不同的小数位,并能够总计,小计

    物料数量字段,根据物料类型的不同,来显示不同的小数位:要求有点苛刻: 首先,要能够总计和小计的话,这一列的字段类型必须是数值类型. 这样的话,就不能通过截取的方式改变不同行的小数位. 以下是两种思路: ...

  9. anriod TabHost

    package com.example.yanlei.mytk; import android.os.Bundle; import android.support.v7.app.AppCompatAc ...

  10. Android 程序目录及UI的简介

    Android程序的目录结构 src:源码的组织管理目录. gen:自动生成的目录,会生成一些重要的文件,如R.java,该目录一般不需要我们编写. assets:该目录文件不会被编译成二进制编码,多 ...