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


相关推荐

  • 注册邮箱发送短信验证_网易邮箱注册系统未收到短信

    注册邮箱发送短信验证_网易邮箱注册系统未收到短信分享概要:讲述yii框架,用户注册然后以邮箱通知和短信通知用户注册状态。短信使用阿里云,邮箱使用swiftmailer插件。支持php~~~功能点:用户注册通知用户注册类:publicfunctionactionCreateUsers(){//数据过滤数据判断这个省略了。。。。直接看重点if($model->save()){//对用户数据的保存…

    2022年10月13日
    0
  • PhpSpreadsheet_php标准输入流stdin

    PhpSpreadsheet_php标准输入流stdin实例化对象composer导包”phpoffice/phpspreadsheet”:”^1.11″,$spreadsheet=newSpreadsheet();$sheet=$spreadsheet->getActiveSheet();合并单元格$sheet->mergeCells(‘A1:J1’);设置单元格的内容$spreadsheet->getActiveSheet()->setCellValue(‘A2′,’序号’)-&

    2022年9月17日
    0
  • java的treemap_java map用法

    java的treemap_java map用法TreeMap简介TreeMap是一个有序的key-value集合,它是通过红黑树实现的。TreeMap继承于AbstractMap,所以它是一个Map,即一个key-value集合。TreeMap实现了NavigableMap接口,意味着它支持一系列的导航方法。比如返回有序的key集合。TreeMap实现了Cloneable接口,意味着它能被克隆。TreeMap实现了ja…

    2025年5月24日
    0
  • 【工具使用】应用层DNS协议工具—iodine

    【工具使用】应用层DNS协议工具—iodine0x01iodine 工具介绍 iodine 工具是基于 C 语言开发的 分为服务端程序 iodined 和客户端 iodine iodine 支持 EDNS base32 base64 base128 等多种编码规范 iodine 支持转发模式和中继模式 iodine 原理 通过 TAP 虚拟网卡 在服务端建立一个局域网 在客户端 通过 TAP 建立一个虚拟网卡 两者通过 DNS 隧道连接 处于同一局域网 可以通过 ping 命令通信 在客户端和服务端之间建立连接后 客户机上会多出一块 dns0 的虚拟网卡 DNS 隧道

    2025年6月21日
    1
  • grep awk sed_shell grep -v

    grep awk sed_shell grep -vRedflagLinux备份软件测试情况一、测试环境1.备份服务器硬件平台A.    CPU B.    内存C.    硬盘2.备份客户端硬件平台A.    CPU B.    内存C.    硬盘3.软件平台A)     操作系统RedFlagDC4.1RedhatAS4Windows2003EnterpriseServerB)     Domino数据库Domin

    2022年8月20日
    3
  • 各主流浏览器内核介绍[通俗易懂]

    各主流浏览器内核介绍[通俗易懂]所谓的“浏览器内核”无非指的是一个浏览器最核心的部分——“RenderingEngine”,直译这个词汇叫做“渲染引擎”,不过我们也常称其为“排版引擎”、“解释引擎”。这个引擎的作用是帮助浏览器来渲

    2022年8月1日
    2

发表回复

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

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