478人阅读 评论(0)

# 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.....对应的分别有几种操作

#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)

// #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;
}


1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：25653次
• 积分：1420
• 等级：
• 排名：千里之外
• 原创：111篇
• 转载：2篇
• 译文：0篇
• 评论：4条
博客专栏
 数据结构 in SDUT 文章：0篇 阅读：0
 机器学习实战 文章：13篇 阅读：2744
阅读排行