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:\n", 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.\n");
                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\n", ans);
                }
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/KirinSB/p/10051292.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值