zoukankan      html  css  js  c++  java
  • HDU 5929 Basic Data Structure 模拟

    Basic Data Structure

    Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 2200    Accepted Submission(s): 477


    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,atop1,,a1 is corresponding to the element of the Stack from top to the bottom, value=atop nand atop1 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.
     
    Input
    The first line contains only one integer T (T20), which indicates the number of test cases.

    For each test case, the first line contains only one integers N (2N200000), 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.
     
    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
    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.
     
    Source
     
     
    双端队列加数组模拟
    查询的时候找到最后一个零的位置可以直接推出答案
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <iomanip>
    #include <math.h>
    #include <map>
    using namespace std;
    #define FIN     freopen("input.txt","r",stdin);
    #define FOUT    freopen("output.txt","w",stdout);
    #define INF     0x3f3f3f3f
    #define INFLL   0x3f3f3f3f3f3f3f
    #define lson    l,m,rt<<1
    #define rson    m+1,r,rt<<1|1
    typedef long long LL;
    typedef pair<int, int> PII;
    using namespace std;
    
    int p[600000];
    
    int lef = 300000, righ = 300001;
    
    int main() {
        //FIN
        int T;
        int cnt = 1;
        scanf("%d", &T);
        while(T--) {
            int n;
            deque<int> deq;
            lef = 300000;
            righ = 300001;
            char op[10];
            printf("Case #%d:
    ", cnt++);
            scanf("%d", &n);
            int flag = 1;
            while(n--) {
                scanf("%s", op);
                int num;
                if(op[0] == 'P' && op[1] == 'U') {
                    scanf("%d", &num);
                    if(flag) {
                        if(num == 0) deq.push_front(lef);
                        p[lef] = num;
                        lef--;
                    }
                    else {
                        if(num == 0) deq.push_back(righ);
                        p[righ] = num;
                        righ++;
                    }
                } else if(op[0] == 'P' && op[1] == 'O') {
                    if(flag) {
                        lef++;
                        if(p[lef] == 0) deq.pop_front();
                    }
                    else {
                        righ--;
                        if(p[righ] == 0) deq.pop_back();
                    }
                } else if(op[0] == 'R') {
                    if(flag) flag = 0;
                    else flag = 1;
                } else if(op[0] == 'Q') {
                    if(righ - 1 == lef) {
                        printf("Invalid.
    ");
                    }
                    else if(deq.empty()) {
                        int ans = (righ - lef - 1) % 2;
                        printf("%d
    ", ans);
                    }
                    else {
                        if(flag) {
                            int tmp = deq.back();
                            //cout << tmp <<"  flag "<<righ - tmp - 1<<endl;
                            tmp = righ - tmp - 1 + (deq.back() != lef + 1);
                            printf("%d
    ", tmp % 2);
                        }
                        else {
                            int tmp = deq.front();
                            //cout << tmp <<" !flag "<< lef <<endl;
                            tmp = tmp - lef - 1 + (deq.front() != righ - 1);
                            printf("%d
    ", tmp % 2);
                        }
    
    
                    }
    
    
                }
            }
    
    
        }
        return 0;
    }
    

      

  • 相关阅读:
    java 中文排序 中文拼音排序 pinyin4j (怡,阿等) 拂晓风起
    jQuery 和 json 简单例子(注意callback函数的处理!!) (servlet返回json,jquery更新,java json) 拂晓风起
    推荐一个免费在线制作Banner的好地方
    Jquery焦点图/幻灯片效果 插件 KinSlideshow
    C#关于伪静态页面的两种实现方法
    cu3er 3D幻灯切换效果 div被遮住的解决方法
    推荐一个亲子教学网站,悟空学字
    怎么通过小米账号查出买家的手机号?
    添加网页桌面快捷方式的代码
    卖小米资格号怎么才不会受骗,怎么才不会淘宝退款?
  • 原文地址:https://www.cnblogs.com/Hyouka/p/7351251.html
Copyright © 2011-2022 走看看