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


相关推荐

  • containsKey使用方法[通俗易懂]

    containsKey使用方法[通俗易懂]作用是判断Map中是否有所需要的键值,下面是具体的代码:

    2022年7月3日
    39
  • pycharm规范快捷键_pycharm修改快捷键

    pycharm规范快捷键_pycharm修改快捷键在写程序的过程中常常会有代码不整齐不规范的警告这时候用pycharn快速规整代码的快捷键为Ctrl+Alt+L即可解决

    2022年8月26日
    5
  • ideaIU-2021.9 激活码_最新在线免费激活

    (ideaIU-2021.9 激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~83PVI25FMO-eyJsaWNlbnNlSWQiOi…

    2022年3月27日
    91
  • 硬件工程师成长之路(9)——检测标准

    硬件工程师成长之路(9)——检测标准系列文章目录1.元件基础2.电路设计3.PCB设计4.元件焊接6.程序设计文章目录前言一、防爆认证前言送给大学毕业后找不到奋斗方向的你(每周不定时更新)嵌入式系统设计师考试一、防爆认证详细资料………………

    2025年9月17日
    9
  • [深度学习] FM & FFM 算法基本原理

    [深度学习] FM & FFM 算法基本原理在推荐系统和计算广告业务中,点击率CTR(click-throughrate)和转化率CVR(conversionrate)是衡量流量转化的两个关键指标。准确的估计CTR、CVR对于提高流量的价值,增加广告及电商收入有重要的指导作用。无论使用什么类型的模型,点击率这个命题可以被归纳到二元分类的问题,我们通过单个个体的特征,计算出对于某个内容,是否点击了,点击了就是1,没点击就是0。对于任何二元分类的问题,最后我们都可以归结到逻辑回归上面。早期的人工特征工程+LR(Logisti…

    2022年5月1日
    68
  • origin绘图软件安装包及入门使用

    origin绘图软件安装包及入门使用1、安装包(2018版)origin是大多被数科研人员选择的数据绘图软件,功能齐全,简单易用,百度云:链接:https://pan.baidu.com/s/1fQRtfwczwye8MfPDi6BmrQ提取码:y72a安装过程及破解方法比较简单自行搜索2、软件界面介绍打开软件如下图所示,1、book用来存放实验数据,如果有多个Y值可以点击工具栏中“列”来添加更多的列值。表格中的数据可以直接从excle中复制进来,简单易用。2、绘图:在book中加入数据后,选中数据选择左下…

    2022年6月9日
    40

发表回复

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

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