zoukankan      html  css  js  c++  java
  • HDU 5929 Basic Data Structure(模拟 + 乱搞)题解

    题意:给定一种二进制操作nand,为

    0 nand 0 = 1
    0 nand 1 = 1 
    1 nand 0 = 1 
    1 nand 1 = 0

    现在要你模拟一个队列,实现PUSH x 往队头塞入x,POP队尾退出,REVERSE翻转,QUERY询问队头到队尾的nand和。

    思路:其他都可以模拟,但是n为2e5,如果直接求nand和必超时,找规律可得,任何串和0nand都为1,那么我们维护一个双端队列保存0出现的位置,然后每次就计算离队尾最近的0和队尾间有几个1。

    代码:

    #include<set>
    #include<map>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    typedef long long ll;
    using namespace std;
    const int maxn = 200000 + 100;
    const int seed = 131;
    const ll MOD = 109;
    const int INF = 0x3f3f3f3f;
    deque<int> q;   //0
    int main(){
        int t, x, l, r, ca = 1, cnt;
        char order[10];
        scanf("%d", &t);
        while(t--){
            printf("Case #%d:
    ", ca++);
            int n;
            int turn = 1;
            cnt = 0;
            l = 0, r = -1;
            while(!q.empty()) q.pop_back();
            scanf("%d", &n);
            while(n--){
                scanf("%s", order);
                if(order[2] == 'S'){    //push
                    scanf("%d", &x);
                    if(turn){
                        cnt++;
                        r++;
                        if(x == 0) q.push_back(r);
                    }
                    else{
                        cnt++;
                        l--;
                        if(x == 0) q.push_front(l);
                    }
                }
                else if(order[2] == 'P'){   //pop
                    if(turn){
                        if(!q.empty() && q.back() == r) q.pop_back();
                        r--;
                    }
                    else{
                        if(!q.empty() && q.front() == l) q.pop_front();
                        l++;
                    }
                    cnt--;
                }
                else if(order[2] == 'V'){   //reverse
                    turn ^= 1;
                }
                else{   //query
                    int ans;
                    if(cnt == 0) printf("Invalid.
    ");
                    else{
                        if(q.empty()) ans = (r - l + 1) % 2;
                        else{
                            if(turn)
                                ans = (q.front() - l + 1 - (q.front() == r)) % 2;
                            else
                                ans = (r - q.back() + 1 - (q.back() == l)) % 2;
                        }
                        printf("%d
    ", ans);
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    区分服务器和客户端,玩家的控制权
    分割字符串
    switch语句的使用
    博客暂停使用
    [题解]洛谷P1041 传染病控制
    [题解]洛谷P2668 斗地主
    [题解]洛谷P4017 最大食物链计数
    [题解]洛谷P1983 车站分级
    [OI学习笔记]倍增LCA
    [OI学习笔记]st表
  • 原文地址:https://www.cnblogs.com/KirinSB/p/10051292.html
Copyright © 2011-2022 走看看