2016CCPC东北地区大学生程序设计竞赛 H题 Basic Data Structure (模拟+双端队列)


Basic Data Structure

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


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.

123


【双端队列】



双端队列 deque    

头文件 <deque>

和队列类似 定义  deque<int> Q;

双端队列,   两头 都可以 进队出队,  默认 左边为 front  右边为 back

如果一边进出的话  就是一个栈了;


【用法】   (默认 你已经知道 queue, stack 的 基本用法)

Q.push_back()// 右边进

Q.push_front()//左边进

Q.pop_back()//右出

Q.pop_front();//左出

Q.front();//左头

Q.back()//右头

Q.empty()//判空



【这道题思路】


如果单纯模拟的话  会超时,  翻转和 查询一定会超时,  为了 缩短时间  在翻转 出 用双端队列, 

可以发现,  与非运算    0 1==1   0 0 =1   1 0 ==1    1 1 =0  只要有0  就-==1   ,   随便写几个示例 可以发现  从 右边开始进行 与非运算,   遇到0  一定可以 变成1

即 有   从右边开始  遇到0 那么 0(包含0)右边的结果是1  ;

所以结果 就出在   最后一个0 的左边 有几个1    奇数 还是偶数的问题;

所以 要 记录 从底i端开始 第一个0 的位置;  但是 又存在翻转  所以我们得记录 顶端 第一个0 的位置;

又因为 存在 pop   所以 我们 又得 记录所有0 的 位置 ;   mmp

说了半天 就是   记录0 的位置  特别标记 是不是第一就行了

问题又出现了,  我查询的时候 要知道 是 双端队列的  哪一边 头?尾 ?    所以用 flag  标记   翻转一次  flag 同时翻转;

【注意】 小细节,    当正向时,  底端 在 最左边 ,  用front ;  反向是 底端 在右边  用back   

front 和back  不要 混了


【代码实现】


//#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define S1(n)    scanf("%d",&n)
#define SL1(n)   scanf("%I64d",&n)
#define S2(n,m)  scanf("%d%d",&n,&m)
#define SL2(n,m)  scanf("%I64d%I64d",&n,&m)
#define Pr(n)     printf("%d\n",n)
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r

using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=500005;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};

ll inv[maxn*2];
inline void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);};}
inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
inline ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
inline ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
inline ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
inline ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
inline ll inv2(ll b){return qpow(b,MOD-2);}

int n;
 struct double_queue{
    deque<int> S;
    int l,r,flag;
    int a[MAXN];
    void init()
    {
        flag=1;
        l=n;// 从n的位置 开始 向前 和向后
        r=n-1;
        while(!S.empty())
            S.pop_back();
    }
    void query()
    {
        if(S.empty())
        {
            if(r<l)// 没有值了
            {
                printf("Invalid.\n");
                return;
            }
            int num=r-l+1;
            if(num&1)
                printf("1\n");
            else
                printf("0\n");

        }
        else
        {
            if(flag)//正向  底端在左边 找 第一个0 的位置
            {
                int fr=S.front();//
                int num=fr-l;//  底端到 第一个0 之间1的个数
                if(num&1)// 奇数
                {
                    if(fr==r) //第一个0 的位置 在 最顶端
                        printf("1\n");
                    else
                        printf("0\n");// 0 后面还有1
                }
                else
                {
                    if(fr==r)   // 偶数正好相反2
                        printf("0\n");
                    else
                        printf("1\n");
                }
            }
            else  // 反向  底端在右边
            {
                int fr=S.back();
                int num=r-fr; //r 一定比fr 大
                if(num&1)
                {
                    if(fr==l)
                        printf("1\n");
                    else
                        printf("0\n");
                }
                else
                {
                    if(fr==l)
                        printf("0\n");
                    else
                        printf("1\n");
                }
            }
        }

    }
    void Push(int num)
    {
        if(flag)//正向  记录存储的值,  当0的时候 位置进队列
        {
            a[++r]=num;
            if(num==0)
                S.push_back(r);
        }
        else  // 反向  记录
        {
            a[--l]=num;
            if(num==0)
                S.push_front(l);
        }
    }
    void Pop()
    {
        if(flag) //正向 出队列
        {
            if(a[r]==0)
                S.pop_back();
            r--;
        }
        else//反向出队列
        {

            if(a[l]==0)
                S.pop_front();
            l++;
        }
    }
    int REV()
    {
        flag^=1;
    }
}deq;

int main()
{
    int t;
    int y;
    cin>>t;
    int cot=0;
    while(t--)
    {
        cin>>n;
        deq.init();
        printf("Case #%d:\n",++cot);
        while(n--)
        {
            char op[10];
            scanf("%s",op);
            if(op[2]=='S')//push
            {
                scanf("%d",&y);
                deq.Push(y);
            }
            else if(op[2]=='V')//reverse
                deq.REV();
            else if(op[2]=='P')//pop
                deq.Pop();
            else if(op[2]=='E')//query
                deq.query();
        }
    }
    return 0;
}



posted @ 2017-10-31 20:04  Sizaif  阅读(280)  评论(0编辑  收藏  举报