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)

    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 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.
     
    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
    对于一个询问只需要求最近的0的位置
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    const int N=4e5+100,M=4e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    char ch[10];
    int main()
    {
        int T,cas=1;
        scanf("%d",&T);
        while(T--)
        {
            printf("Case #%d:
    ",cas++);
            int st=200010,en=200010,f=0;
            int n;
            scanf("%d",&n);
            set<int>s;
            set<int>::iterator it;
            for(int i=1;i<=n;i++)
            {
                scanf("%s",ch);
                if(ch[0]=='P')
                {
                    if(ch[1]=='U')
                    {
                        int x;
                        scanf("%d",&x);
                        if(x==0)
                            s.insert(en);
                        if(f)
                            en--;
                        else
                            en++;
                    }
                    else
                    {
                        if(f)
                        {
                            if(!s.empty())
                            {
                                it=s.begin();
                                if(*it<=en+1)
                                s.erase(it);
                            }
                            en++;
                        }
                        else
                        {
                            if(!s.empty())
                            {
                                it=s.end();
                                it--;
                                if(*it>=en-1)
                                s.erase(it);
                            }
                            en--;
                        }
                    }
                }
                else if(ch[0]=='Q')
                {
                    if(st==en)
                    {
                        printf("Invalid.
    ");
                    }
                    else if(s.empty())
                    {
                        printf("%d
    ",abs((en-st)%2));
                    }
                    else if(f)
                    {
                        int p;
                        it=s.end();
                        it--;
                        p=*it;
                        int ans=st-p;
                        if(p>en+1)ans++;
                        printf("%d
    ",ans%2);
                    }
                    else
                    {
                        int p;
                        it=s.begin();
                        p=*it;
                        int ans=p-st;
                        if(p<en-1)ans++;
                        printf("%d
    ",ans%2);
                    }
                }
                else
                {
                    if(f)
                    {
                        int temp=st;
                        st=en+1;
                        en=temp+1;
                        f=0;
                    }
                    else
                    {
                        int temp=st;
                        st=en-1;
                        en=temp-1;
                        f=1;
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    【BZOJ 4631】4631: 踩气球 (线段树)
    【BZOJ 4148】 4148: [AMPPZ2014]Pillars (乱搞)
    【LYOI 212】「雅礼集训 2017 Day8」价(二分匹配+最大权闭合子图)
    【BZOJ 4104】 4104: [Thu Summer Camp 2015]解密运算 (智商)
    【BZOJ 4103】 4103: [Thu Summer Camp 2015]异或运算 (可持久化Trie)
    【BZOJ 3747】 3747: [POI2015]Kinoman (线段树)
    【BZOJ 3997】 3997: [TJOI2015]组合数学 (DP| 最小链覆盖=最大点独立集)
    【BZOJ 3727】 3727: PA2014 Final Zadanie (递推)
    【BZOJ 3442】 3442: 学习小组 (最大费用流)
    【BZOJ 3218】 3218: a + b Problem(最小割+可持久化线段树)
  • 原文地址:https://www.cnblogs.com/jhz033/p/5937204.html
Copyright © 2011-2022 走看看