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

 

 

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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 硬件介绍CPU显卡内存[通俗易懂]

    硬件介绍CPU显卡内存[通俗易懂]一,CPU主频:这是一个最受新手关注的指标,指的就是CPU内核工作的时钟频率(CPUClockSpeed)。通常所说的某款CPU是多少兆赫兹的,而这个多少兆赫兹就是“CPU的主频”。在学校经常听见一些人问,XXX网吧的CPU2.66G!XXX网吧的才2G,有人用2.66G的赛扬与2.0G-2.66G的P4比,这是无知的表现,和他们争是无意义的:)。主频虽与CPU速度有关系,但确对不是绝对的正比…

    2022年6月20日
    35
  • java parrallel for,Java 8 parallel forEach进度指示

    java parrallel for,Java 8 parallel forEach进度指示ForperformancereasonIwouldliketouseaforEachloopofaparallelLambdastreaminordertoprocessaninstanceofaCollectioninJava.AsthisrunsinabackgroundServiceIwouldliketouse…

    2022年7月19日
    21
  • windows下CUDA的卸载以及安装

    windows下CUDA的卸载以及安装参考博客windows7下cuda9.0卸载、cuda8.0安装一、前言对于一个刚玩CUDA菜鸟来说,安装问题就是一个巨大的坑,安装过程里面有很多需要注意的细节,很多自定义的选项,如果漏选,对编译GPU版本的Caffe会出现一些莫名奇妙的问题。为此,会经常出现卸载CUDA,再安装CUDA的问题,所以对于CUDA的卸载与安装就会有一些注意事项,现在总结一下。二、…

    2022年9月23日
    2
  • zencart和php是什么,什么是ZenCart ZenCart有什么特点 ZenCart模板

    zencart和php是什么,什么是ZenCart ZenCart有什么特点 ZenCart模板什么是ZenCart?ZenCart有什么特点,ZenCart模板什么是ZenCart?ZenCart是免费的购物车软件-用于建立自己的网上商店,为网上销售商而设计。ZenCart是一个免费、界面友好,开放式源码的购物车软件。该软件由一些销售商、程序员、设计师和顾问们共同开发,目的就是用户能建立风格不同的电子商务系统。现有的一些解决方案过重于编程,而不是着眼于客户的需求。ZenCart把销售商…

    2022年7月27日
    3
  • 海思hi3516ev100开发板_海思V200

    海思hi3516ev100开发板_海思V2001安装ubunu14我的ubuntu14如下#uname-aLinuxubuntu4.4.0-142-generic#168~14.04.1-UbuntuSMPSatJan1911:26:28UTC2019x86_64x86_64x86_64GNU/Linux2软件包安装步骤1.配置默认使用bash执行sudodpkg-recon…

    2022年9月23日
    1
  • HTML5实战与剖析之HTMLDocument变化(readyreState属性、兼容模式和head属性)「建议收藏」

    HTML5实战与剖析之HTMLDocument变化(readyreState属性、兼容模式和head属性)「建议收藏」之前为大家介绍了些许关于HTML5新添加的小东东,想必大家也有所了解了。今天为大家介绍HTML5中有关HTMLDocument方面新添加的内容。那么HTML5中有关HTMLDocument方面新添加的内容都有什么呢?HTML5中有关HTMLDocument方面新添加的内容有readyState属性、兼容模式判断和head属性。下面就为大家一一介绍这些新添加的小东东吧。

    2022年7月19日
    20

发表回复

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

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