Basic Data Structure hdu 5929
来源:互联网 发布:软件推广ppt 编辑:程序博客网 时间:2024/05/21 19:47
Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1366 Accepted Submission(s): 338
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.
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:
By the way, NAND is a basic binary operation:
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.
For each test case, the first line contains only one integers N (
In the following N lines, the i-th line contains one of these operations below:
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
28PUSH 1QUERYPUSH 0REVERSEQUERYPOPPOPQUERY3PUSH 0REVERSEQUERY
Sample Output
Case #1:11Invalid.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.
Source
2016CCPC东北地区大学生程序设计竞赛 - 重现赛
#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;int s[450000];int z[450000];int main(){ int t; scanf("%d", &t); for(int cas = 1; cas <= t; cas++) { memset(z,0,sizeof(z)); int n; scanf("%d", &n); printf("Case #%d:\n",cas); char ss[10]; int f = 1;/// 1 zheng; 0 fu; int l = 250000; int r = l; int ll = 250000;int rr = ll; int x; int len =0, len1 = 0; while(n--) { scanf("%s",ss); if(ss[0] == 'P' &&ss[1] == 'U') { scanf("%d", &x); if(f) { s[r++] = x; if(x == 0) z[rr++] = r - 1,len1++; } else { s[--l] = x; if(x == 0) z[--ll] = l,len1++; } len++; } if(ss[0] == 'P' && ss[1] == 'O') { if(f) { if(s[r-1] == 0) { rr--,len1--; } r--; } else { if(s[l] == 0) { ll++,len1--; } l++; } len--; } if(ss[0] == 'R') { f = !f; } if(ss[0] == 'Q') { //printf("l: %d r: %d len:%d\n",z[ll],z[rr-1],len); if(len<=0) printf("Invalid.\n"); else if(len1==0) printf("%d\n",len%2); else { if(f) { z[ll] == r - 1 ? printf("%d\n",(len - 1)%2):printf("%d\n",(z[ll] - l + 1) % 2); } else { z[rr - 1] == l ? printf("%d\n",(len - 1) % 2):printf("%d\n",(r - z[rr - 1])%2);; } } } } } return 0;}
0 0
- hdu 5929 Basic Data Structure
- Basic Data Structure hdu 5929
- HDU-5929 Basic Data Structure
- HDU 5929Basic Data Structure
- [HDU 5929] Basic Data Structure (脑洞题)
- hdu 5929 Basic Data Structure(山东CCPC)
- hdu 5929 Basic Data Structure 模拟
- HDU 5929 Basic Data Structure (模拟)
- HDU - 5929 Basic Data Structure(模拟+规律)
- H - Basic Data Structure HDU - 5929
- Basic Data Structure HDU 5929(模拟)
- HDU 5292 Basic Data Structure
- hdu 5929 Basic Data Structure(找规律,模拟)
- hdu 5929 Basic Data Structure 双端队列
- HDU 5929 Basic Data Structure (找规律)
- hdu 5929 CCPC东北四省赛H - Basic Data Structure
- hdu 5929 Basic Data Structure(双端队列+模拟)
- hdu 5929 Basic Data Structure 找规律 模拟
- C++实现多态原理探究
- 单词统计案例
- 【t101】小明搬家
- Android读取指定路径的位图(FR:徐海涛)
- Follow me!百万奖金由你拿 | 精准资助机器学习(三)
- Basic Data Structure hdu 5929
- Python批量Excel文件数据导入SQLite数据库的优化方案
- 深入理解子元素的width与父元素的width关系
- java中string.format与stringbuilder的性能比较
- 寒假21:整数的拆分
- 游戏服务端之C++封装Mysql
- C++Student类
- 网站设计器v1.0-20170215版本发布记录
- AngularJs服务-http请求