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,⋯,a1atop,atop−1,⋯,a1 is corresponding to the element of the Stack from top to the bottom, value=atopvalue=atop nand atop−1atop−1 nand ... nand a1a1. 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. 

InputThe first line contains only one integer T (T≤20T≤20), which indicates the number of test cases.

For each test case, the first line contains only one integers N (2≤N≤2000002≤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.
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 Input

  1. 2
  2. 8
  3. PUSH 1
  4. QUERY
  5. PUSH 0
  6. REVERSE
  7. QUERY
  8. POP
  9. POP
  10. QUERY
  11. 3
  12. PUSH 0
  13. REVERSE
  14. QUERY

Sample Output

  1. Case #1:
  2. 1
  3. 1
  4. Invalid.
  5. Case #2:
  6. 0

Hint

  1. 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
  2. (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.
  3.  
  4. 给一个栈.有push,pop,query ,reverse这些操作,
    对于每个询问输出这个栈从栈顶到底进行题目给的这个运算后的结果;
    ∙∙ 0 nand 0 = 1 
    ∙∙ 0 nand 1 = 1 
    ∙∙ 1 nand 0 = 1 
    ∙∙ 1 nand 1 = 0 
  5.  
  6. 组队训练赛的时候这题我直接扔给队友写的
    队友写的自闭了 不过最后还是出来了
    自己赛后补题 补到自闭
    我不适合写模拟
  7.  
  8. 我写了很久 尽量的简化了 模拟过程
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <set>
  7. #include <iostream>
  8. #include <map>
  9. #include <stack>
  10. #include <string>
  11. #include <vector>
  12. #define pi acos(-1.0)
  13. #define eps 1e-6
  14. #define fi first
  15. #define se second
  16. #define lson l,m,rt<<1
  17. #define rson m+1,r,rt<<1|1
  18. #define bug printf("******\n")
  19. #define mem(a,b) memset(a,b,sizeof(a))
  20. #define fuck(x) cout<<"["<<x<<"]"<<endl
  21. #define f(a) a*a
  22. #define sf(n) scanf("%d", &n)
  23. #define sff(a,b) scanf("%d %d", &a, &b)
  24. #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
  25. #define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
  26. #define pf printf
  27. #define FRE(i,a,b) for(i = a; i <= b; i++)
  28. #define FREE(i,a,b) for(i = a; i >= b; i--)
  29. #define FRL(i,a,b) for(i = a; i < b; i++)
  30. #define FRLL(i,a,b) for(i = a; i > b; i--)
  31. #define FIN freopen("DATA.txt","r",stdin)
  32. #define gcd(a,b) __gcd(a,b)
  33. #define lowbit(x) x&-x
  34. #pragma comment (linker,"/STACK:102400000,102400000")
  35. using namespace std;
  36. typedef long long LL;
  37. typedef unsigned long long ULL;
  38. const int INF = 0x7fffffff;
  39. const int mod = 1e9 + ;
  40. const int maxn = 4e5 + ;
  41. int t, n, a[maxn], L, R, cas = , x;
  42. char s[];
  43. multiset<int>st;
  44. multiset<int>::iterator it;
  45. int main() {
  46. // FIN;
  47. sf(t);
  48. while(t--) {
  49. st.clear();
  50. L = 2e5, R = 2e5;
  51. int flag = ;
  52. sf(n);
  53. printf("Case #%d:\n", cas++);
  54. for (int i = ; i < n ; i++) {
  55. scanf("%s", s);
  56. if (s[] == 'Q') {
  57. if (L == R) printf("Invalid.\n");
  58. else if (st.empty()) printf("%d\n",(R-L)%);
  59. else {
  60. if (flag) {
  61. //fuck((*st.begin()) - L);
  62. printf("%d\n", (((*st.begin()) - L) + (*st.begin() != R - )) % );
  63. } else {
  64. it=st.end();
  65. it--;
  66. // fuck(L);
  67. printf("%d\n", ((R - (*it))- + (*it != L)) % );
  68. }
  69. }
  70. }
  71. if (s[] == 'P' && s[] == 'U') {
  72. sf(x);
  73. if (flag) a[R++] = x;
  74. else a[--L] = x;
  75. if (!x) {
  76. if (flag) st.insert(R - );
  77. else st.insert(L);
  78. }
  79. }
  80. if (s[] == 'P' && s[] == 'O') {
  81. if (L == R) continue;
  82. if (flag) {
  83. if(!a[R - ]) st.erase(R - );
  84. R--;
  85. } else {
  86. if (!a[L]) st.erase(L);
  87. L++;
  88. }
  89. }
  90. if (s[] == 'R') flag ^= ;
  91. }
  92. }
  93. return ;
  94. }

Basic Data Structure HDU - 5929 (这个模拟我要报警了)的更多相关文章

  1. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  3. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  4. Basic Data Structure

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  5. 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 ...

  6. hdu 5929 Basic Data Structure

    ゲート 分析: 这题看出来的地方就是这个是左结合的,不适用结合律,交换律. 所以想每次维护答案就不怎么可能了.比赛的时候一开始看成了异或,重读一遍题目了以后就一直去想了怎么维护答案...... 但是很 ...

  7. 【推导】【线段树】hdu5929 Basic Data Structure

    题意: 维护一个栈,支持以下操作: 从当前栈顶加入一个0或者1: 从当前栈顶弹掉一个数: 将栈顶指针和栈底指针交换: 询问a[top] nand a[top-1] nand ... nand a[bo ...

  8. 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  9. HDU 2217 Data Structure?

    C - Data Structure? Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. linux学习总结----mongoDB总结

    dbhelper.py 用户登录和注册(加密算法) 加密导包 import hashlib 或者使用Md5 加密 MongoDB ->JSON service mysql start servi ...

  2. python同时遍历两个list

    两个list, 有对应关系,希望同时完成遍历 用迭代器迭代的方法也不是不可以,python提供了更直观的方法: 可以使用zip把两个list打包 , 类似: list1 = [1,2,3,4] lis ...

  3. OpenMPI 集群配置

    现在有2台机器,希望可以尝试一下在多台机器上跑MPI的感觉,所以跑之前就得配置,先参考网址: https://www.cnblogs.com/awy-blog/p/3402949.html: 1. 配 ...

  4. ubuntu server guide 学习笔记

    1. 软件包 1.1. dpkg dpkg -l dpkg -l | grep apache2 dpkg -L ufw dpkg -S /etc/host.conf dpkg -i zip_3.0-4 ...

  5. UVa 1585 - Score - ACM/ICPC Seoul 2005 解题报告 - C语言

    1.题目大意 给出一个由O和X组成的字符串(长度为80以内),每个O的得分为目前连续出现的O的数量,X得分为0,统计得分. 2.思路 实在说不出了,这题没过脑AC的.直接贴代码吧.=_= 3.代码 # ...

  6. 火狐metamask账号

    火狐metamask lock trophy pyramid sunny aim inmate body sense sing castle cinnamon cram

  7. C Program基础-宏定义

    写好c语言,漂亮的宏定义是非常重要的,我们在阅读别人工程时,往往能看到大量的宏定义,宏定义可以增加代码的可读性,也能增加代码的可移植性,一个好的宏定义甚至是一件艺术品.今天我们就来看看宏定义的方方面面 ...

  8. Reversing Encryption(模拟水题)

    A string ss of length nn can be encrypted(加密) by the following algorithm: iterate(迭代) over all divis ...

  9. 感谢信——Alpha版

    作为Thunder团队的leader,当时担任组长,说实话,确实是头脑一热,可后来,在确定选题时,看着大家都有自己的想法,看着大家都那么踊跃,而我因为性格的原因,总是难以做决定,导致选题这件事就开了几 ...

  10. Mininet实验 MAC地址学习

    实验目的 了解交换机的MAC地址学习过程. 了解交换机对已知单播.未知单播和广播帧的转发方式. 实验原理 MAC(media access control,介质访问控制)地址是识别LAN节点的标识.M ...