Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1046 Accepted Submission(s): 272
∙ 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.
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.
直接模拟肯定超时,注意到与非操作,不论1还是0,与非0都等于1,也就是说只要记录最靠近栈底部的那个0,设从栈顶到这个0之前的与非结果为x,x nand 0 = 1恒成立,因为记录的这个0是最后一个0了,所以这个0到栈底都是1,然后看最后这连续的1是偶数个还是奇数个就可以判断最后的结果是0还是1了
实现起来有好多细节要注意:
1.栈为空的时候
2.元素全为1的时候
3.距离栈底最近的一个0在栈顶的时候
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = (200000 << 1) + 5;
//head和tail为实际的栈的两端
//a[l],a[r]分别为靠左端最近的0的位置,和靠右端最近的0的位置
int T, n, kase, a[maxn], l, r, head, tail, flag, x, ans;
char op[15];
int main()
{
cin >> T;
kase = 0;
while (T--) {
scanf("%d", &n);
//head和tail都可以为负值,因为计算长度的时候只需要知道相对位置的差值就行了
//head和tail本身的正负无所谓,但是r和l是要表示a的下标的,不能为负值
//flag记录反转信息
r = 200000; l = r + 1; head = 0; tail = 1; flag = 1;
printf("Case #%d:\n", ++kase);
while (n--) {
scanf("%s", op);
if (op[2] == 'S') {
scanf("%d", &x);
if (flag) {
++head;
if (!x) a[++r] = head; //如果PUSH 0那么记录新的0的位置
}
else {
--tail;
if (!x) a[--l] = tail;
}
}
else if (op[2] == 'P') {
if (flag) {
--head;
if (l <= r && a[r] > head) --r; //如果弹出的是0,那么最右端的0的位置要往左一个
}
else {
++tail;
if (l <= r && a[l] < tail) ++l;
}
}
else if (op[0] == 'R') {
flag ^= 1;
}
else if (op[0] == 'Q') {
if (head < tail) puts("Invalid."); //栈空
else if (flag) {
if (r < l) { //r < l说明没有0,全是1
if ((head - tail + 1) & 1) {
puts("1");
}
else {
puts("0");
}
}
else {
if (head > a[l]) { //栈顶不是0
if ((a[l] - tail) & 1) {
puts("0");
}
else {
puts("1");
}
}
else {
//因为head不可能小于a[l](因为POP时的这句if (l <= r && a[r] > head) --r;)
//保证了tail到head之间不存在比head大的a[i]
//所以此时是head == a[l],0为栈顶
if ((a[l] - tail) & 1) {
puts("1");
}
else {
puts("0");
}
}
}
}
else {
if (r < l) {
if ((head - tail + 1) & 1) {
puts("1");
}
else {
puts("0");
}
}
else {
if (tail < a[r]) {
if ((head - a[r]) & 1) {
puts("0");
}
else {
puts("1");
}
}
else {
if ((head - a[r]) & 1) {
puts("1");
}
else {
puts("0");
}
}
}
}
}
}
}
return 0;
}