Basic Data Structure HDU 5929(模拟)

原创 2017年02月18日 09:23:33

Basic Data Structure

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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 doNAND 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 not change 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.

题意 :

模拟栈给你定义几种操作

push pop REVERSE QUERY.....对应的分别有几种操作

后两个比较重要  1是栈翻转操作 2是询问操作

首先1  我的做法是用数组模拟  (体会到了数据结构的重要性) top bottom 记录临时的头尾  比较大小就可以得知谁是头尾

其次2  这是本t的重点难点 遍历一遍肯定超时 我们可以看到与非逻辑关系 只要有0不管和谁运算都出现1 利用这个性质记录栈中最后出栈的0的位置

例如 top11011110bottom   因为栈底是0 那么答案就是1  top1100111011bottom 判断0后面出现1的奇偶性就可

又例如 top01111bottom

还有 top0000bottom  以及top1111bottom top0bottom 这几种特殊情况



#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>

using namespace std;

#define For(i,a,b) for(i=a;i<=b;i++)
#define _For(i,a,b) for(i=b;i>=a;i--)
#define Out(x) cout<<x<<endl
#define Outdouble(x,a) cout<<fixed<<setprecision(a)<<1.0*x<<endl
#define pf printf
#define sf scanf
#define mset(arr,num) memset(arr,num,sizeof(arr))

#define ll long long
const ll inf = 800010; ///
#define ok std::ios::sync_with_stdio(0)
#pragma comment(linker, "/STACK:102400000,102400000")

// #define debug
#if defined (debug)
---check---
#endif

/// ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^ ///
const int start = 300000;

bool arr[inf];

int bottom = start;
int top = bottom + 1;
int oddsum = 0;
int sum;

deque <int> q;

void Push(int y)
{
    sum++;
    bool x = (bool)y;
    if(top < bottom)
    {
        arr[top] = x;
        if(x == 0)
        {
            q.push_front(top);  ///left
        }
        top--;
    }
    else
    {
        arr[top] = x;
        if(x == 0)
        {
            q.push_back(top);  ///right
        }
        top++;
    }

}

void Reve()
{
    swap(top,bottom);
}

void Que()
{
    int num;
    if(abs(top-bottom) == 1)
    {
        pf("Invalid.\n");  ///empty
        return ;
    }
    if(q.size() == 0)  ///all of deque is 1
    {
        pf("%d\n",sum%2);
        return ;
    }
    if(top < bottom)
    {
        if(q.back() == bottom - 1) ///序列最后(back)一个是0那么答案是1/0
        {
            if(sum > 1)
            {
                pf("1\n");
            }
            else if(sum == 1)
            {
                pf("0\n");
            }
            return ;
        }

        num = bottom - 1 - q.back();
        if(q.back() == top + 1)  ///如果back是top元素说明前面无1  奇1偶0
        {
            if(num%2 == 1)
            {
                pf("1\n");
            }
            else pf("0\n");
        }
        else
        {
            if(num%2 == 1)
            {
                pf("0\n");
            }
            else pf("1\n");
        }
    }
    else ///top > bottom
    {
        if(q.front() == bottom + 1) ///最前(front)一个是0那么答案是1
        {
            if(sum > 1)
            {
                pf("1\n");
            }
            else if(sum == 1)
            {
                pf("0\n");
            }
            return ;
        }

        num = q.front() - bottom -1; ///--17:18
        if(q.front() == top - 1) ///如果front是top元素说明前面无1 奇1偶0
        {
            if(num%2 == 1)
            {
                pf("1\n");
            }
            else pf("0\n");
        }
        else
        {
            if(num%2 == 1)
            {
                pf("0\n");
            }
            else pf("1\n");
        }
    }
}

void Pop()
{
    sum--;
    if(top < bottom)
    {
        top++;
        if(arr[top] == 0)
        {
            q.pop_front();
        }
    }
    else
    {
        top--;
        if(arr[top] == 0)
        {
            q.pop_back();
        }
    }
}

int main()
{
    int i,cnt = 0;;
    int t,n;
    char s[20];
    sf("%d",&t);
    while(t--)
    {
        bottom = start;
        top = bottom + 1;
        sum = 0;
        cnt++;
        q.clear();

        sf("%d",&n);
        pf("Case #%d:\n",cnt);
        For(i,1,n)
        {
            sf("%s",s);
            if(s[0] == 'P' && s[1] == 'U')
            {
                int x;
                cin>>x;
                Push(x);
            }
            else if(s[0] == 'P' && s[1] == 'O')
            {
                Pop();
            }
            else if(s[0] == 'R')
            {
                Reve();
            }
            else if(s[0] == 'Q')
            {
                Que();
            }
        }
    }
    return 0;
}


版权声明:有错误麻烦赐教,感激不尽~~~(转载留言哦~)

相关文章推荐

hdu 5929 Basic Data Structure 模拟

// hdu 5929 Basic Data Structure 模拟 // 题目链接: // http://acm.hdu.edu.cn/showproblem.php?pid=59...

HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/...

【HDU5929 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 H】【打表找规律 队列模拟】Basic Data Structure 双端栈下连续1和0做nand的结果

Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot...

hdu 5929 Basic Data Structure 找规律 模拟

Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operati...

双端队列+与非运算——Basic Data Structure ( HDU 5929 )

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5929 分析: 实现一个数据结构,能够满足如下四种操作: PUSH:把一个元素( 0 or 1 ...
  • FeBr2
  • FeBr2
  • 2016-10-07 11:07
  • 392

hdu 5929 CCPC东北四省赛H - Basic Data Structure

题意: 定义一个可以反转栈顶的栈,有push,pop操作,每次push只push 0,1这两种元素,除此之外还有reverse操作,即把栈顶反转为栈尾, 栈尾反转为栈顶,另外有一个query操作,查...

HDU Basic Data Structure 2016CCPC东北地区大学生程序设计竞赛 - 重现赛

Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth...

Light OJ 1303——Basic Data Structure

调了一下午的水题,一开始是题目读错了……然后把queue直接用数组模拟,调了好久还是不对,直到前面才发现是pos = check(w, l, r) == -1出错了,==是先执行的,这样的话pos就被...

Basic Data Structure(弱校联盟十一专场)

Question:题目详情(http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1008&cid=729) 题目大意:有一个栈有四个...

UVA11995 I Can Guess the Data Structure!(数据结构,模拟)

I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)