Basic Data Structure

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

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.

 
题意:维护一个栈,支持往栈里塞 0/1 ,弹栈顶,翻转栈,询问从栈顶到栈底按顺序 NAND 的值。
题解:只要知道最后的 0后面 1的个数的奇偶性就行。
我这里是用set 存储0的位置 比较麻烦 也可以和存储0/1一样 维护一个 0的位置 的栈
感谢评论区 hack数据
1
10
PUSH 0
REVERSE
QUERY
PUSH 1
PUSH 1
REVERSE
POP
QUERY
POP
QUERY

  代码中已经标记更改

 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
//typedef long long ll;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int t;
char str[];
map<int,int> mp;
int l,r;
int l0,r0;
int exm;
int flag=;
int n;
set<int>se;
set<int>::iterator it;
int biao=;
int main()
{
scanf("%d",&t);
{
for(int k=; k<=t; k++)
{
se.clear();
mp.clear();
biao=;
scanf("%d",&n);
l=r=;
printf("Case #%d:\n",k);
for(int i=; i<=n; i++)
{
scanf("%s",str);
if(strcmp(str,"PUSH")==)
{
scanf("%d",&exm);
if(exm==)
se.insert(l); mp[l]=exm;
if(biao==)
l++;
else
l--;
}
if(strcmp(str,"POP")==)
{
if(biao==)
l--;
else
l++;
}
if(strcmp(str,"REVERSE")==)
{
if(biao==)
{
l--;
r--;
swap(l,r);
biao=;
}
else
{
l++;
r++;
swap(l,r);
biao=;
}
}
if(strcmp(str,"QUERY")==)
{
if(l==r)
printf("Invalid.\n");
else
{
if(biao==)
{
int exm;
int gg=;
for(it=se.begin(); it!=se.end(); it++)
{
if(*it>=r)
{
exm=*it;
gg=;
break;
}
}
if(exm>=l)/*评论区hack点 更正 */
gg=;
if(gg==)
{
if((l-r)%==)
printf("0\n");
else
printf("1\n");
}
else
{
if(l==r+)
printf("%d\n",mp[exm]);
else
{
if(exm==(l-))
exm--;
if((exm-r+)%)
printf("1\n");
else
printf("0\n");
}
}
}
else
{
int exm;
int gg=;
if(se.size()!=)
{
it=--se.end();
for(;; it--)
{
if(*it<=r)
{
exm=*it;
gg=;
break;
}
if(it==se.begin())
break;
}
}
if(exm<=l)/*评论区hack点 更正*/
gg=;
if(gg==)
{
if((r-l)%==)
printf("0\n");
else
printf("1\n");
}
else
{
int hhh=;
if(l==r-)
printf("%d\n",mp[exm]);
else
{
if(exm==(l+))
exm++;
if((r-exm+)%)
printf("1\n");
else
printf("0\n");
}
}
}
}
}
}
}
}
return ;
}
/*
2
6
PUSH 1
PUSH 1
PUSH 1
PUSH 1
REVERSE
QUERY
5
PUSH 1
PUSH 1
PUSH 1
PUSH 1
QUERY
*/

2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟的更多相关文章

  1. 2016CCPC东北地区大学生程序设计竞赛 1008 HDU5929

    链接http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:给你一种数据结构以及操作,和一种位运算,最后询问:从'栈'顶到低的运算顺序结果是多少 解法:根据 ...

  2. HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

    Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  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. HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Mr. Frog's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  5. HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  7. HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)

    Auxiliary Set Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  8. 2016CCPC东北地区大学生程序设计竞赛 (2018年8月22日组队训练赛)

    题目链接:http://acm.hdu.edu.cn/search.php?field=problem&key=2016CCPC%B6%AB%B1%B1%B5%D8%C7%F8%B4%F3%D ...

  9. 2016CCPC东北地区大学生程序设计竞赛 1005 HDU5926

    链接http://acm.hdu.edu.cn/showproblem.php?pid=5926 题意:给我们一个矩阵,问你根据连连看的玩法可以消去其中的元素 解法:连连看怎么玩,就怎么写,别忘记边界 ...

随机推荐

  1. 常见JS(JavaScript)冲突解决方法

    1.一般JS冲突解决办法 a.最容易出现的就是js的命名冲突 ①.变量名冲突 变量有全局变量和局部变量当全局变量变量和局部变量名称一致时,就会js冲突,由于变量传递数值或地址不同就会产生JavaScr ...

  2. UVa 11375 - Matches

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  3. 警卫安排(dp好题)

    警卫安排(guard)[题目描述]一个重要的基地被分为 n 个连通的区域.出于某种神秘的原因,这些区域以一个区域为核心,呈一颗树形分布.在每个区域安排警卫所需要的费用是不同的,而每个区域的警卫都可以望 ...

  4. html中offsetTop、clientTop、scrollTop、offsetTop

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

  5. [开发笔记]-多线程异步操作如何访问HttpContext?

    如何获取文件绝对路径? 在定时器回调或者Cache的移除通知中,有时确实需要访问文件,然而对于开发人员来说, 他们并不知道网站会被部署在哪个目录下,因此不可能写出绝对路径, 他们只知道相对于网站根目录 ...

  6. JSON基础使用

    1)JSON概念 JSON 是纯文本 JSON 具有“自我描述性”(人类可读) JSON 具有层级结构(值中存在值) JSON 可通过 JavaScript 进行解析 JSON 数据可使用 AJAX ...

  7. app store 上架流程

    前言:作为一名IOS开发者,把开发出来的App上传到App Store是必须的.下面就来详细介绍下具体流程. 1.打开苹果开发者中心:https://developer.apple.com 打开后点击 ...

  8. 移动设备和SharePoint 2013 - 第3部分:推送通知

    博客地址:http://blog.csdn.net/foxdave 原文地址 在该系列文章中,作者展示了SharePoint 2013最显著的新功能概观--对移动设备的支持. 该系列文章: 移动设备和 ...

  9. Netstat命令(一)

    一.查看那些端口号被占用,在命令行中录入:netstat -an ,下图已查找80端口为例 二.查看哪个程序在使用80端口 三.查看占用80端口所对应的PID号 四.打开任务管理器,可以根据PID号找 ...

  10. php练习题:投票

    通过连接数据库,对数据库的增删改来实现一个投票的进行与结果的显示: 方法一: 主页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...