HDU 5929 Basic Data Structure 模拟

HDU 5929 Basic Data Structure 模拟

大家好,又见面了,我是全栈君。

Basic Data Structure

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

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 notchange 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 (
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.

 

 

Source
 
 
双端队列加数组模拟
查询的时候找到最后一个零的位置可以直接推出答案
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN     freopen("input.txt","r",stdin);
#define FOUT    freopen("output.txt","w",stdout);
#define INF     0x3f3f3f3f
#define INFLL   0x3f3f3f3f3f3f3f
#define lson    l,m,rt<<1
#define rson    m+1,r,rt<<1|1
typedef long long LL;
typedef pair<int, int> PII;
using namespace std;

int p[600000];

int lef = 300000, righ = 300001;

int main() {
    //FIN
    int T;
    int cnt = 1;
    scanf("%d", &T);
    while(T--) {
        int n;
        deque<int> deq;
        lef = 300000;
        righ = 300001;
        char op[10];
        printf("Case #%d:\n", cnt++);
        scanf("%d", &n);
        int flag = 1;
        while(n--) {
            scanf("%s", op);
            int num;
            if(op[0] == 'P' && op[1] == 'U') {
                scanf("%d", &num);
                if(flag) {
                    if(num == 0) deq.push_front(lef);
                    p[lef] = num;
                    lef--;
                }
                else {
                    if(num == 0) deq.push_back(righ);
                    p[righ] = num;
                    righ++;
                }
            } else if(op[0] == 'P' && op[1] == 'O') {
                if(flag) {
                    lef++;
                    if(p[lef] == 0) deq.pop_front();
                }
                else {
                    righ--;
                    if(p[righ] == 0) deq.pop_back();
                }
            } else if(op[0] == 'R') {
                if(flag) flag = 0;
                else flag = 1;
            } else if(op[0] == 'Q') {
                if(righ - 1 == lef) {
                    printf("Invalid.\n");
                }
                else if(deq.empty()) {
                    int ans = (righ - lef - 1) % 2;
                    printf("%d\n", ans);
                }
                else {
                    if(flag) {
                        int tmp = deq.back();
                        //cout << tmp <<"  flag "<<righ - tmp - 1<<endl;
                        tmp = righ - tmp - 1 + (deq.back() != lef + 1);
                        printf("%d\n", tmp % 2);
                    }
                    else {
                        int tmp = deq.front();
                        //cout << tmp <<" !flag "<< lef <<endl;
                        tmp = tmp - lef - 1 + (deq.front() != righ - 1);
                        printf("%d\n", tmp % 2);
                    }


                }


            }
        }


    }
    return 0;
}

  

转载于:https://www.cnblogs.com/Hyouka/p/7351251.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108242.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • webpack图片压缩_webpack图片打包加速

    webpack图片压缩_webpack图片打包加速图片处理url-loader(webpack5之前的处理方式)在项目开发中,我们时长会需要使用到图片,比如在img文件夹中有图片test1.png,然后在normal.css中会引用到图片body

    2022年8月7日
  • TOF相机基本知识

    TOF相机基本知识TOF是Timeofflight的简写,直译为飞行时间的意思。所谓飞行时间法3D成像,是通过给目标连续发送光脉冲,然后利用传感器接收从物体返回的光,通过探测光脉冲的飞行时间来得到目标物的距离。TOF的深度精度不随距离改变而变化,基本能稳定在cm级。1TOF相机的特点:相对二维图像,可通过距离信息获取物体之间更加丰富的位置关系,即区分前景与后景深度信息依旧可以完成对目标图像

    2022年5月9日
  • 算法分析与设计论文

    算法分析与设计论文1:递归算法程序直接或间接调用自身的编程技巧称为递归算法(Recursion)。递归算法是一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法。它通常把一个大型复杂的问题转化为一个与原问题

    2022年8月2日
  • 虚拟机扩容磁盘后扩容分区_如何将磁盘主分区设置为活动分区

    虚拟机扩容磁盘后扩容分区_如何将磁盘主分区设置为活动分区当服务器数据太多的时候,硬盘不足的时候就得考虑扩容,为了不影响业务的正常运行,一般云服务器的本地磁盘都是不支持分区的,因为业务数据通常是不能中断和移动的,无论是增加硬盘或在原有磁盘增加分区的方式扩容,势必会存在卸载、挂载、移动等操作。所以对于云硬盘,如果我们要把他作为数据盘。即使能分区,也最好不要分区,以免以后扩容麻烦。以下用虚拟机看一下对未分区磁盘扩容的效果[root@k8s-node01…

    2022年8月30日
  • c语言中switch是什么语句_switch结构c语言

    c语言中switch是什么语句_switch结构c语言c语言中switch的用法是:功能:switch语句是多分支选择语句.用来实现多分支选择结构.if语句只有两个分支可供选择,而实际问题中常常要用到多分支的选择.例如,学生成绩分类(90为”…

    2022年8月30日
  • myeclipse6.5注册码

    myeclipse6.5注册码出处:http://www.cnblogs.com/qinqinmeiren/archive/2011/08/07/2151673.html做Java软件开发的,首选的软件工具莫非是MyEclipse

    2022年7月2日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号