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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章