Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2057 Accepted Submission(s): 451
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,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 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.
∙ 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 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 (
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.
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: 0HintIn 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
Recommend
wange2014
题意:一共有n个操作,操作分为4种:PUSH x(x:0或1),POP,REVERSE(翻转栈里面的数),QUERY(假使栈内的数一个个出栈,并按出栈顺序将他们与非起来,问最终结果(注意,并不是真的出栈))
解题思路:可以用双端队列模拟,队列里放0和每段1的个数,根据与非的性质,1、0与非0都为1,所以查询的时候就判断最近0的位置和1的数量就可以。
若最后一个数为0,那么答案必为1
若最后一个数不为0:若队列里元素小于等于2,则最后一段1的数量为奇数答案为1,否则为0,若队列里元素大于2,则最后一段1的数量为奇数答案为0,否则为1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int INF = 0x3f3f3f3f3f;
#define LL long long
int main()
{
int t, cas = 0;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
deque<int>q;
char ch[20];
int x, flag = 0;
printf("Case #%d:\n", ++cas);
for (int i = 1;i <= n;i++)
{
scanf("%s", ch);
if (!strcmp(ch, "PUSH"))
{
scanf("%d", &x);
if (!flag)
{
if (x == 0) q.push_back(x);
else
{
if (!q.empty() && q.back()!= 0)
{
int k = q.back() + 1;
q.pop_back();
q.push_back(k);
}
else q.push_back(x);
}
}
else
{
if (x == 0) q.push_front(x);
else
{
if (!q.empty() && q.front() != 0)
{
int k = q.front() + 1;
q.pop_front();
q.push_front(k);
}
else q.push_front(x);
}
}
}
else if (!strcmp(ch, "POP"))
{
if (!flag)
{
if (q.back() == 0) q.pop_back();
else
{
int k = q.back()-1;
q.pop_back();
if(k>0) q.push_back(k);
}
}
else
{
if (q.front() == 0) q.pop_front();
else
{
int k = q.front() - 1;
q.pop_front();
if (k>0) q.push_front(k);
}
}
}
else if (!strcmp(ch, "REVERSE")) flag ^= 1;
else
{
if (q.empty()) printf("Invalid.\n");
else
{
if (flag)
{
if (q.back() == 0 && q.size() > 1) printf("1\n");
else if (q.back() == 0) printf("0\n");
else if (q.size() <= 2)
{
if (q.back() % 2 == 1) printf("1\n");
else printf("0\n");
}
else
{
if (q.back() % 2 == 1) printf("0\n");
else printf("1\n");
}
}
else
{
if (q.front() == 0 && q.size() > 1)
printf("1\n");
else if (q.front() == 0)
printf("0\n");
else if (q.size() <= 2)
{
if (q.front() % 2 == 1) printf("1\n");
else printf("0\n");
}
else
{
if (q.front() % 2 == 1) printf("0\n");
else printf("1\n");
}
}
}
}
}
}
return 0;
}