HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 872 Accepted Submission(s): 236Problem DescriptionMr. 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 = 0Because 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.
InputThe 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
∙ QUERYIt is guaranteed that the current stack will not be empty while doing POP operation.
OutputFor 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 Input2
8
PUSH 1
QUERY
PUSH 0
REVERSE
QUERY
POP
POP
QUERY
3
PUSH 0
REVERSE
QUERYSample OutputCase #1:
1
1
Invalid.
Case #2:
0HintIn 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.SourceRecommend
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5929
题目大意:
一个栈,N个操作,支持4种操作
1.压入0或1
2.弹出栈顶
3.将栈倒置(底和顶对换)
4.询问,A[top] nand A[top-1] nand ... nand A[base]。
0 nand 0=0 nand 1=1 nand 0 = 1, 1 nand 1 =1
题目思路:
【模拟】
nand运算的特征是0和任何数nand结果是1,所以当A[i]=0时,如果i上面还有元素,可以全部忽略,ans[i]=1,如果没有元素,ans[i]=0
所以只要看栈底往上的连续的1个个数,根据奇偶个数判断答案是1还是0,注意只有1个元素的情况、1个0的情况。
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 100004
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int b,t;
int a[N*][];
char ch[];
void PUSH()
{
int i,j,x;
scanf("%d",&x);
n++;
if(n==)
a[++t][]=x,a[t][]=;
else if(a[t][]==x)
a[t][]++;
else if(b>t)
a[--t][]=x,a[t][]=;
else
a[++t][]=x,a[t][]=;
}
void POP()
{
if(!n)return;
n--;
if(--a[t][]==)
{
if(b>t)t++;
else t--;
}
}
void REVERSE()
{
if(!n)return;
swap(b,t);
}
void QUERY()
{
if(!n)puts("Invalid.");
else if(n==)
printf("%d\n",a[b][]);
else
{
if(a[b][])
{
if(n<=a[b][]+)printf("%d\n",a[b][]&);
else printf("%d\n",a[b][]&^);
}
else puts("");
}
}
int main()
{
#ifndef ONLINE_JUDGEW
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// init();
// for(scanf("%d",&cass);cass;cass--)
for(scanf("%d",&cas),cass=;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d%d",&n,&m))
{
printf("Case #%d:\n",cass);
scanf("%d",&m);
n=;
b=N;t=N-;
for(k=;k<=m;k++)
{
scanf("%s",ch);
if(ch[]=='S')PUSH();
else if(ch[]=='P')POP();
else if(ch[]=='V')REVERSE();
else if(ch[]=='E')QUERY();
}
}
return ;
}
/*
// //
*/
HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)的更多相关文章
- HDU 5929 Basic Data Structure 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 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 ...
- 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 ...
- HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
Coconuts Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
Auxiliary Set Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 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 ...
- 2016CCPC东北地区大学生程序设计竞赛 1008 HDU5929
链接http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:给你一种数据结构以及操作,和一种位运算,最后询问:从'栈'顶到低的运算顺序结果是多少 解法:根据 ...
随机推荐
- 使用linq语句获取指定条数的记录
//获得指定个数的子文件夹,用于分页 var pageAlbums = (from SPFolder pf in lstSubAlbums select pf) ...
- TCP的服务流程
TCP协议提供的是可靠的.面向连接的传输控制协议,即在传输数据前要先建立逻辑连接,然后再传输数据,最后释放连接3个过程.TCP提供端到端.全双工通信:采用字节流方式,如果字节流太长,将其分段:提供紧急 ...
- jsp - forward指令
forward指令 既可以指向静态的html页面,也可以转发到动态的jsp页面,并可以保留先前请求的参数. 例如,在web中新建一个Jsp_src.jsp的jsp页面: <%@ page lan ...
- C语言之分配
#include "stdio.h" void main() { ] = {,,}; *sizeof(int));//malloc calloc relloc arr2[] = ; ...
- iOS UITableviewWrapperView 和 automaticallyAdjustsScrollViewInsets属性
关于在navigationController下面使用tableView在竖直方向会遇到frame的y值的困惑, 会遇到视图控制器的这个属性:automaticallyAdjustsScrollVie ...
- Linux下追踪函数调用,打印栈帧
事情的起因是这样的,之前同事的代码有一个内存池出现了没有回收的情况.也就是是Pop出来的对象没有Push回去,情况很难复现,所以在Pop里的打印日志,跟踪是谁调用了它,我想在GDB调试里可以追踪调用的 ...
- Windows Phone 之下拉菜单ListPicker
默认情况下,Visual Studio的ToolBox里没有任何下拉菜单的控件可供使用,虽然可以手工输入代码使用隐藏的ComboBox来实现下拉菜单,但是显示出来的菜单与Metro UI主题不匹配.S ...
- js 判断一个点是否在一个多边形之内
出处: https://github.com/substack/point-in-polygon/blob/master/index.js github: https://github.com/sub ...
- jcarousellite 实现图片列表滚动
jcarousel Lite与jcarousel 相比去掉了皮肤的约束,可以自定义的设置前后按钮和选项卡按钮,生成的html更加简洁,文件更小(2k) 下载链接:http://www.gmarwaha ...
- SNN--Second Name Node
NameNode有这么几个配置: Property Description Suggested value dfs.name.dir Directory in NameNode’s local fil ...