hdu5929Basic Data Structure

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5929

题意:要求模拟一个存入0,1的栈,有4个操作1:push a,在栈顶插入a。2:pop,删除栈顶。3:reverse,将这个栈翻转一下。4:query,询问栈顶到栈底的atop nand atop-1 nand ... a1。

分析:前3个操作都很容易实现,唯一的难度就是快速就是这个答案。我们会发现这个题目给出的运算法则是不满足结合律的,那么我们想用什么分段,合并之类的加速就无法实现。然后我们仔细观察会发现我们想要快速计算一段a[1] nand a[2]....nand a[n],我们只需要找到距离n最近的那个0的位置即可,因为从运算法则可以看出前面无论答案是0/1与0运算一次都会变成1,那么剩下的就是一连串的1了,那么答案就只和1的个数的奇偶有关了。实现有些小细节要处理一下,不会的就看代码好了。O(n)

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=400010;
const int M=50010;
const int mod=1000000007;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double eps=1e-5;
const double inf=~0u>>1;
const double pi=acos(-1.0);
typedef long double db;
typedef unsigned int uint;
typedef unsigned long long ull;
char s[10];
int l,r,f[N];
int main()
{
    int a,i,g,n,dir,hea,tai,ca,T;
    scanf("%d", &T);
    for (ca=1;ca<=T;ca++) {
        scanf("%d", &n);
        hea=0;tai=1;dir=1;
        r=200000;l=r+1;
        printf("Case #%d:\n", ca);
        while (n--) {
            scanf("%s", s);
            if (s[2]=='S') {
                scanf("%d", &a);
                if (dir) {
                    hea++;
                    if (!a) f[++r]=hea;
                } else {
                    tai--;
                    if (!a) f[--l]=tai;
                }
            } else if (s[2]=='P') {
                    if (dir) {
                        hea--;
                        if (l<=r&&f[r]>hea) r--;
                    } else {
                        tai++;
                        if (l<=r&&f[l]<tai) l++;
                    }
                } else if (s[2]=='V') dir^=1;
                    else {
                        if (tai>hea) printf("Invalid.\n");
                        else if (l>r) printf("%d\n", (hea-tai+1)&1);
                            else {
                                if (dir) {
                                    g=f[l]-tai;
                                    if (hea>f[l]) g++;
                                    printf("%d\n", g&1);
                                } else {
                                    g=hea-f[r];
                                    if (f[r]>tai) g++;
                                    printf("%d\n", g&1);
                                }
                            }
                    }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值