HDU 5929 Basic Data Structure 模拟
Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
∙ 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.
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.
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.)
8
PUSH 1
QUERY
PUSH 0
REVERSE
QUERY
POP
POP
QUERY
3
PUSH 0
REVERSE
QUERY
1
1
Invalid.
Case #2:
0
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.
#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 模拟的更多相关文章
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 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 ...
- hdu 5929 Basic Data Structure
ゲート 分析: 这题看出来的地方就是这个是左结合的,不适用结合律,交换律. 所以想每次维护答案就不怎么可能了.比赛的时候一开始看成了异或,重读一遍题目了以后就一直去想了怎么维护答案...... 但是很 ...
- Basic Data Structure HDU - 5929 (这个模拟我要报警了)
Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operati ...
- hdu-5929 Basic Data Structure(双端队列+模拟)
题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- Basic Data Structure
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 【推导】【线段树】hdu5929 Basic Data Structure
题意: 维护一个栈,支持以下操作: 从当前栈顶加入一个0或者1: 从当前栈顶弹掉一个数: 将栈顶指针和栈底指针交换: 询问a[top] nand a[top-1] nand ... nand a[bo ...
- HDU 2217 Data Structure?
C - Data Structure? Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
随机推荐
- 图书管理之HTML5压缩旋转裁剪图片总结
整体思路 : 在移动端压缩图片并且上传主要用到filereader.canvas 以及 formdata 这三个h5的api.逻辑并不难.整个过程就是: (1)用户使用input file上传图片的 ...
- Android TextView中 字体加粗方法
textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗 textView.getPaint().setFakeBoldT ...
- Codeforces 733C:Epidemic in Monstropolis(暴力贪心)
http://codeforces.com/problemset/problem/733/C 题意:给出一个序列的怪兽体积 ai,怪兽只能吃相邻的怪兽,并且只有体积严格大于相邻的怪兽才能吃,吃完之后, ...
- 【转载】使用barman备份PostgreSQL
什么是barman Barman (备份和恢复管理器) 是 PostgreSQL 数据库服务器中非常方便的备份和恢复工具,允许远程备份多个服务器,允许从一个备份集中一个命令就恢复数据库.同时还可以对多 ...
- Oracle性能优化--DBMS_PROFILER
想看到过程或者函数执行每一步的过程:想看到每一步所占的时间吗?借助profiler吧:它可以满足你来分析过程/函数执行比较久:可以直接快速找到病因:从而可以优化那一步需要优化下. 一 ...
- northwind数据库
① Categories: 种类表相应字段:CategoryID :类型ID:CategoryName:类型名;Description:类型说明;Picture:产品样本 ② CustomerCust ...
- hibernate关于一对一用法
首先来说一下数据库的表结构吧.主要涉及到两张表.一张是订单表sub_table 一张是商品表. 之后说entity public class SubTable { private Inte ...
- C#事物执行数据
public class sqlservershiwu { public string sqlconString = "Data Source=.;Initial Catalog=TestD ...
- vi编辑文件E437: terminal capability "cm" required 解决办法
E437: terminal capability "cm" required 这个错误一般是环境变量TERM没有配置或者配置错误所致. 解决办法: 执行export TERM=x ...
- Linux下c++中的atoi、atol、atoll、atof函数调用实例
本文中调用的四个函数如下: atoi函数:将字符串转化为int类型变量 atol函数:将字符串转化为long类型变量 atoll函数:将字符串转化为long long类型变量 atof函数:将字符串转 ...