题目链接:

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. JMS学习(四) Selector详解

    一.前言 在掌握了消息的结构之后,我们接下来看一下JMS的一个重要功能:选择器.有些时候,作为消费者只希望处理自己感兴趣的消息.如果某个消息只有一个消费者,我们可以在让该客户端根据规则来处理自己感兴趣 ...

  2. sqlite3之基本操作(一)

    简单的介绍 SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说没有独立的维护进程,所有的维护都来自于程序本身.它是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经 ...

  3. pygame for python3.3

    pygame的更新慢的令人发指,我最初使用的python是3.4版本的,无何奈何pygame不支持3.4,甚至官网只有3.2版本的.我于是将各种版本试了一遍,出现各种问题,同时我比较钟爱3.x版本,最 ...

  4. 缓存技术比拼:Redis与Memcached的同与不同

    转至:http://developer.51cto.com/art/201603/507980.htm 在今天的文章中,我们将探讨Redis(REmote DIctionary Server).Red ...

  5. windows下redis启动报Creating Server TCP listening socket *:6379: bind: No such file or directory

    解决方法: 按顺序输入如下命令就可以连接成功 # redis-cli.exe# > shutdown# > exit# redis-server.exe redis.windows.con ...

  6. HTML Jquery

    在<网页制作Dreamweaver(悬浮动态分层导航)>中,运用到了jQuery的技术,轻松实现了菜单的下拉.显示.隐藏的效果,不必再用样式表一点点地修改,省去了很多麻烦,那么jQuery ...

  7. New Valid Tracking Metric Now Available in Seller Central

    On July 7, Amazon added Valid Tracking Rate as a new metric in Seller Central. This metric shows the ...

  8. Mysql之执行计划

    1.explain分析sql语句 例如: EXPLAIN )  ORDER BY bi.`publish_time`  返回结果: 而今天检查的不是这条sql,远比这条复杂,不过也能反映情况了. (1 ...

  9. 详解HTML5中rel属性的prefetch预加载功能使用

    在HTML5中,有个很有用但常被忽略的特性,就是预先加载(prefetch),它的原理是: 利用浏览器的空闲时间去先下载用户指定需要的内容,然后缓存起来,这样用户下次加载时,就直接从缓存中取出来,效率 ...

  10. 提高eclipse使用效率(二) 提高Android开发效率的小技巧

    XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Window - Preferences,在右边的目录树中切换到XML - X ...