• Basic Data Structure HDU


    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,atop1,,a1atop,atop−1,⋯,a1 is corresponding to the element of the Stack from top to the bottom, value=atopvalue=atop nand atop1atop−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 (T20T≤20), which indicates the number of test cases. 

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

    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. 
    
            
     
    给一个栈.有push,pop,query ,reverse这些操作,
    对于每个询问输出这个栈从栈顶到底进行题目给的这个运算后的结果;
    ∙ 0 nand 0 = 1 
    ∙ 0 nand 1 = 1 
    ∙ 1 nand 0 = 1 
    ∙ 1 nand 1 = 0 




    组队训练赛的时候这题我直接扔给队友写的
    队友写的自闭了 不过最后还是出来了
    自己赛后补题 补到自闭
    我不适合写模拟


    我写了很久 尽量的简化了 模拟过程

     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("******
    ")
    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 + 7;
    40 const int maxn = 4e5 + 10;
    41 int t, n, a[maxn], L, R, cas = 1, x;
    42 char s[10];
    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 = 1;
    52         sf(n);
    53         printf("Case #%d:
    ", cas++);
    54         for (int i = 0 ; i < n ; i++) {
    55             scanf("%s", s);
    56             if (s[0] == 'Q') {
    57                 if (L == R) printf("Invalid.
    ");
    58                 else if (st.empty()) printf("%d
    ",(R-L)%2);
    59                 else {
    60                     if (flag) {
    61                         //fuck((*st.begin()) - L);
    62                         printf("%d
    ", (((*st.begin()) - L) + (*st.begin() != R - 1)) % 2);
    63                     } else {
    64                         it=st.end();
    65                         it--;
    66                      //   fuck(L);
    67                         printf("%d
    ", ((R - (*it))-1 + (*it != L)) % 2);
    68                     }
    69                 }
    70             }
    71             if (s[0] == 'P' && s[1] == 'U') {
    72                 sf(x);
    73                 if (flag) a[R++] = x;
    74                 else a[--L] = x;
    75                 if (!x) {
    76                     if (flag)  st.insert(R - 1);
    77                     else st.insert(L);
    78                 }
    79             }
    80             if (s[0] == 'P' && s[1] == 'O') {
    81                 if (L == R) continue;
    82                 if (flag) {
    83                     if(!a[R - 1]) st.erase(R - 1);
    84                     R--;
    85                 } else {
    86                     if (!a[L]) st.erase(L);
    87                     L++;
    88                 }
    89             }
    90             if (s[0] == 'R') flag ^= 1;
    91         }
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    Android系统的Binder机制之一——Service Manager
    Android系统的Binder机制之二——服务代理对象(1)
    电子书
    [ 具体数学 ] 1:递归式与封闭式
    线段树封装
    实验1——顺序表的合并
    配置终端VIM 文件
    编译原理:实验二、集合与线性表操作
    括号匹配检验
    Problem B: KMP算法综合应用余庆
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9520611.html
Copyright © 2020-2023  润新知