HDU - 5929 - Basic Data Structure
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,atop−1,⋯,a1 is corresponding to the element of the Stack from top to the bottom, value=atop nand atop−1 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 (T≤20), which indicates the number of test cases.
For each test case, the first line contains only one integers N (2≤N≤200000), 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.
题目大意:
模拟题
一个栈 有入栈 出栈 翻转栈 求值四个操作
求值 是从栈顶到栈顶按
这个规则来计算。
栈中元素只有0 | 1
解题思路
我们观察这个计算规则,发现只要有0 那么结果一定是1
那么如果我们从栈顶到栈底来计算值得话, 从栈底往上看 只要碰到0就不用继续往上看了,因为上面计算的结果在加上这个0 肯定就变成1 了(如果0上面没别的数了 它的结果就是0 如果还有就是1)
所以我们只要记录0的位置即可。
用一个数组来模拟这个栈的操作。然后用个双向队列来记录0的位置。
具体看代码就懂了
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 5;
int a[maxn*2];//模拟双端队列
deque<int> q;
int l,r;
int n;
int size=0;
inline void push_0(int num){
a[++r] = num;
if(num == 0){
q.push_back(r);
}
size++;
}
inline void push_1(int num){
a[--l] = num;
if(num==0){
q.push_front(l);
}
size++;
}
inline void pop_0(){
if(a[r]==0){
q.pop_back();
}
r--;
size--;
}
inline void pop_1(){
if(a[l]==0){
q.pop_front();
}
l++;
size--;
}
void query0(){
if(size==0){
printf("Invalid.\n");
return;
}
if(q.size()==0){
printf("%d\n",(r-l+1)%2);
return ;
}
if (q.front() == r)//分两种情况考虑,最后一个0不会变成1
printf("%d\n", (r - l) % 2);
else
printf("%d\n", (q.front() - l + 1) % 2);//因为最后一个0也会变成1,所以1的数量要加1
}
inline void query1(){
if(size==0){
printf("Invalid.\n");
return;
}
if(q.size()==0){
printf("%d\n",(r-l+1)%2);
return ;
}
if (q.back() == l)
printf("%d\n", (r - l) % 2);
else
printf("%d\n", (r - q.back() + 1) % 2);
}
int main(){
int t;
cin>>t;
int cas = 1;
while(t--){
scanf("%d",&n);
printf("Case #%d:\n",cas++);
q.clear();
l = maxn ;
r = maxn-1;
size = 0;
int now = 0;//0代表正序,1代表倒序
while(n--){
char op[20];
scanf("%s", op);
if(strcmp(op, "PUSH") == 0){
int num;
scanf("%d",&num);
if(now == 0){
push_0(num);
}else{
push_1(num);
}
}else if(op[0] == 'Q'){
if(now == 0){
query0();
}else{
query1();
}
}else if(op[0] == 'R'){
now = (now+1)%2;
}else if(strcmp(op, "POP") == 0){
if(size==0) continue;
if(now == 0){
pop_0();
}else{
pop_1();
}
}
}
}
return 0;
}