1. 程式人生 > >HDU Basic Data Structure 2016CCPC東北地區大學生程式設計競賽

HDU Basic Data Structure 2016CCPC東北地區大學生程式設計競賽

Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 28    Accepted Submission(s): 0


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,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 = 10 nand 1 = 11 nand 0 = 11 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 (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. 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.

序列最後一個值為0時

ans=0        //序列長度為1  
ans=1        //other                       

所以 用一個雙向佇列記錄序列裡0的下標
query時
當序列是正向時 找到序列中最右邊的一個0的下標 此時序列該下標右邊全是1
例如
1011 0 111->1 111
0 1111 -> 1 111
此時的值會不斷地0,1,0,1,0迴圈 判斷1的個數即可

反向也的同理

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
//#define CHECK_TIME

const int N=200000+3;

struct S{
    int elem[2*N];
    bool reverse;
    int st,end;
    deque<int>index_of_0;
    S(){
        reverse=false;
        st=end=N;
    }
    void push(int x){
        if(reverse){
            elem[--st]=x;
            if(x==0)
                index_of_0.push_front(st);
        }
        else{
            elem[end]=x;
            if(x==0)
                index_of_0.push_back(end);
            ++end;
        }
    }
    void pop(){
        if(reverse){
            if(index_of_0.front()==st)
                index_of_0.pop_front();
            ++st;
        }
        else{
            if(index_of_0.back()==end-1)
                index_of_0.pop_back();
            --end;
        }
    }

    void reverseS(){
        reverse=false==reverse;
    }
    bool empty(){
        return end==st;
    }
    int size(){
        return end-st;
    }
    int query(){
        if(empty())
            return -1;
        if(size()==1)
            return elem[st];
        if(index_of_0.empty())
            return size()&1;
        if(!reverse){//注意方向問題 在此WA 4次....
            if(index_of_0.front()!=end-1)
                return (index_of_0.front()-st+1)&1;
            else
                return (index_of_0.front()-st)&1;
        }
        else{
            if(index_of_0.back()!=st)
                return (end-index_of_0.back())&1;
            else
                return ((end-index_of_0.back())&1)==0;
        }
    }
    void clear(){
        st=end=N;
        index_of_0.clear();
        reverse=false;
    }
};

int main()
{
    //freopen("/home/lu/文件/r.txt","r",stdin);
    //freopen("/home/lu/文件/w.txt","w",stdout);
    int T,n,x;
    char in[6];
    S mystack;
    scanf("%d",&T);
    for(int t=1;t<=T;++t){
        scanf("%d",&n);
        printf("Case #%d:\n",t);
        mystack.clear();
        for(int i=0;i<n;++i){
            scanf("%s",in);
            if(in[2]=='S'){
                scanf("%d",&x);
                mystack.push(x);
            }
            else
                if(in[2]=='P')
                    mystack.pop();
                else
                    if(in[2]=='V')
                        mystack.reverseS();
                    else
                        if(in[2]=='E'){
                            x=mystack.query();
                            if(x<0)
                                printf("Invalid.\n");
                            else
                                printf("%d\n",x);
                        }
        }
    }
    return 0;
}