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

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • timestampdiff的一个BUG

    timestampdiff的一个BUG发现一个timestampdiff无法正确判断列值的BUG,看下面例子:–测试表如下:mysql>select*fromtest;+—-+———————+|id|col2|+—-+———————+|1|2019-03-2000:00:00||2|2…

  • @RequestParam注解使用

    @RequestParam注解使用1、作用:@RequestParam:将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)2、语法:语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)value:参数名required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报…

  • 【Python_环境配置】Pycharm创建虚拟环境

    【Python_环境配置】Pycharm创建虚拟环境问题由来从github下载的模型程序,所适包的版本不同,导致Pycharm中包混乱、版本冲突。 为每个程序单独创建虚拟环境,使得特定程序只能访问虚拟环境中的包,从而保持全局解释器的干净整洁。创建虚拟环境File-Settings-PythonInterpreter-设置图标,后续设置如下:Pycharm之创建虚拟环境在特定虚拟环境中安装包1、选择下方Terminal2、利用cd进入项目的Scripts文件夹3、输入activate4、利用pip命…

    2022年10月30日
  • android之View的启动过程[通俗易懂]

    程序里调用了onSizeChanged方法进行了一些设置,不知道onSizeChanged是在什么时候启动的,所以研究了一下View的启动流程代码如下:public class TestView extends View { public TestView(Context context) { super(context); Log.d(“mDebug”, “TestV

  • PyCharm 2021.10.3 x64 激活码[最新免费获取]

    (PyCharm 2021.10.3 x64 激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

  • 【python实战】不让我复制?看我自制个带文字识别的截屏工具

    【python实战】不让我复制?看我自制个带文字识别的截屏工具人生苦短,我用python

发表回复

您的电子邮箱地址不会被公开。

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