HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
2016-10-21 12:19
387 查看
Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 872 Accepted Submission(s): 236
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.)
[align=left]Sample Input[/align]
2
8
PUSH 1
QUERY
PUSH 0
REVERSE
QUERY
POP
POP
QUERY
3
PUSH 0
REVERSE
QUERY
[align=left]Sample Output[/align]
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.
[align=left]Source[/align]
2016CCPC东北地区大学生程序设计竞赛 - 重现赛
[align=left]Recommend[/align]
wange2014 | We have carefully selected several similar problems for you: 5932 5931 5930 5928 5925
Statistic | Submit | Discuss | Note
题目链接:
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的情况。
1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algorithm> 6 #include<string> 7 #include<iomanip> 8 #include<map> 9 #include<stack> 10 #include<queue> 11 #include<set> 12 #include<bitset> 13 #include<memory.h> 14 #include<time.h> 15 #include<stdio.h> 16 #include<stdlib.h> 17 #include<string.h> 18 //#include<stdbool.h> 19 #include<math.h> 20 #pragma comment(linker,"/STACK:1024000000,1024000000") 21 #define min(a,b) ((a)<(b)?(a):(b)) 22 #define max(a,b) ((a)>(b)?(a):(b)) 23 #define abs(a) ((a)>0?(a):(-(a))) 24 #define lowbit(a) (a&(-a)) 25 #define sqr(a) ((a)*(a)) 26 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) 27 #define mem(a,b) memset(a,b,sizeof(a)) 28 #define eps (1e-10) 29 #define J 10000 30 #define mod 1000000007 31 #define MAX 0x7f7f7f7f 32 #define PI 3.14159265358979323 33 #define N 100004 34 using namespace std; 35 typedef long long LL; 36 double anss; 37 LL aans; 38 int cas,cass; 39 int n,m,lll,ans; 40 int b,t; 41 int a[N*3][2]; 42 char ch[10]; 43 void PUSH() 44 { 45 int i,j,x; 46 scanf("%d",&x); 47 n++; 48 if(n==1) 49 a[++t][0]=x,a[t][1]=1; 50 else if(a[t][0]==x) 51 a[t][1]++; 52 else if(b>t) 53 a[--t][0]=x,a[t][1]=1; 54 else 55 a[++t][0]=x,a[t][1]=1; 56 } 57 void POP() 58 { 59 if(!n)return; 60 n--; 61 if(--a[t][1]==0) 62 { 63 if(b>t)t++; 64 else t--; 65 } 66 } 67 void REVERSE() 68 { 69 if(!n)return; 70 swap(b,t); 71 } 72 void QUERY() 73 { 74 if(!n)puts("Invalid."); 75 else if(n==1) 76 printf("%d\n",a[b][0]); 77 else 78 { 79 if(a[b][0]) 80 { 81 if(n<=a[b][1]+1)printf("%d\n",a[b][1]&1); 82 else printf("%d\n",a[b][1]&1^1); 83 } 84 else puts("1"); 85 } 86 } 87 int main() 88 { 89 #ifndef ONLINE_JUDGEW 90 // freopen("1.txt","r",stdin); 91 // freopen("2.txt","w",stdout); 92 #endif 93 int i,j,k; 94 int x,y,z; 95 // init(); 96 // for(scanf("%d",&cass);cass;cass--) 97 for(scanf("%d",&cas),cass=1;cass<=cas;cass++) 98 // while(~scanf("%s",s)) 99 // while(~scanf("%d%d",&n,&m)) 100 { 101 printf("Case #%d:\n",cass); 102 scanf("%d",&m); 103 n=0; 104 b=N;t=N-1; 105 for(k=1;k<=m;k++) 106 { 107 scanf("%s",ch); 108 if(ch[2]=='S')PUSH(); 109 else if(ch[2]=='P')POP(); 110 else if(ch[2]=='V')REVERSE(); 111 else if(ch[2]=='E')QUERY(); 112 } 113 } 114 return 0; 115 } 116 /* 117 // 118 119 // 120 */
View Code
相关文章推荐
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- (HDU 5929)Basic Data Structure 双端队列+模拟 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- hdu 5929 Basic Data Structure 2016CCPC东北地区大学生程序设计竞赛
- 【HDU5929 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 H】【打表找规律 队列模拟】Basic Data Structure 双端栈下连续1和0做nand的结果
- 2016CCPC东北地区大学生程序设计竞赛 H题 Basic Data Structure (模拟+双端队列)
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5929 Basic Data Structure(模拟,deque)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- hdu 5926 Mr. Frog’s Game 2016CCPC东北地区大学生程序设计竞赛E题(模拟)
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5922 Minimum’s Revenge 【模拟】(2016CCPC东北地区大学生程序设计竞赛)
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU Basic Data Structure 2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- HDU Mr. Frog’s Problem 2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- (HDU 5922)Minimum’s Revenge 思维水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
- 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟
- hdu 5924 Mr. Frog’s Problem(2016CCPC东北地区大学生程序设计竞赛 C题,数学思维)
- hdu 5927 Auxiliary Set 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 F题(树状dp)