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: 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
对于一个询问只需要求最近的0的位置
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=4e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
char ch[];
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
printf("Case #%d:\n",cas++);
int st=,en=,f=;
int n;
scanf("%d",&n);
set<int>s;
set<int>::iterator it;
for(int i=;i<=n;i++)
{
scanf("%s",ch);
if(ch[]=='P')
{
if(ch[]=='U')
{
int x;
scanf("%d",&x);
if(x==)
s.insert(en);
if(f)
en--;
else
en++;
}
else
{
if(f)
{
if(!s.empty())
{
it=s.begin();
if(*it<=en+)
s.erase(it);
}
en++;
}
else
{
if(!s.empty())
{
it=s.end();
it--;
if(*it>=en-)
s.erase(it);
}
en--;
}
}
}
else if(ch[]=='Q')
{
if(st==en)
{
printf("Invalid.\n");
}
else if(s.empty())
{
printf("%d\n",abs((en-st)%));
}
else if(f)
{
int p;
it=s.end();
it--;
p=*it;
int ans=st-p;
if(p>en+)ans++;
printf("%d\n",ans%);
}
else
{
int p;
it=s.begin();
p=*it;
int ans=p-st;
if(p<en-)ans++;
printf("%d\n",ans%);
}
}
else
{
if(f)
{
int temp=st;
st=en+;
en=temp+;
f=;
}
else
{
int temp=st;
st=en-;
en=temp-;
f=;
}
}
}
}
return ;
}

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

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

  3. hdu 5929 Basic Data Structure

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

  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(双端队列+模拟)

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

  6. Basic Data Structure

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

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

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

  8. HDU 2217 Data Structure?

    C - Data Structure? Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  9. 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟

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

随机推荐

  1. 为Docker容器配置固定IP

    当docker以桥接的方式启动容器时,容器内部的IP是经过DHCP获取的,例如:172.17.0.8/32,且每重启依次IP都会发生变动.某些特殊的情况下,需要容器内有自己固定的一个内部IP.我的实现 ...

  2. android 系统相册调用,各版本的区别总结

    请求系统相册有三个Action: (注意以下  图库(缩略图)   和  图片(原图)  的区别) ACTION_OPEN_DOCUMENT    仅限4.4或以上使用  默认打开原图 ACTION_ ...

  3. Spring+Quartz实现定时执行任务的配置

    1.要想使用Quartz 必须要引入相关的包:以下是我在项目中gradle中的配置: compile 'org.quartz-scheduler:quartz:2.1.1' 2.Scheduler的配 ...

  4. [ios]离屏渲染优化

    原文链接:https://mp.weixin.qq.com/s?__biz=MjM5NTIyNTUyMQ==&mid=2709544818&idx=1&sn=62d0d2e9a ...

  5. c#多播委托

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  6. 最流行的编程语言JavaScript能做什么?

    本文转自互联网! 首先很遗憾的一点是,"PHP虽然是最好的语言",但是它不是最流行的语言. 对不起的还有刚刚在4月TIOBE编程语言排行榜上榜的各个语言: 你们都很棒,但是你们都担 ...

  7. apache-common pool的使用

    Apache commons-pool本质上是"对象池",即通过一定的规则来维护对象集合的容器;commos-pool在很多场景中,用来实现"连接池"/&quo ...

  8. ocruntime

    原作: http://www.jianshu.com/p/25a319aee33d 三种方法的选择 Runtime提供三种方式来将原来的方法实现代替掉,那该怎样选择它们呢? Method Resolu ...

  9. 为什么需要main函数,及其参数的用法

    首先,需要明确main函数是什么? 答:main函数是C语言约定的入口函数 C99标准里面是这样描述的: Program startup The function called at program ...

  10. HDU1757 A Simple Math Problem 矩阵快速幂

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...