hdu5929 Basic Data Structure 规律+模拟

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


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,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.


直接模拟肯定超时,注意到与非操作,不论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;
}




发布了198 篇原创文章 · 获赞 8 · 访问量 14万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章