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 doNAND 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.
InputThe 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.
OutputFor 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

题意:

          模拟的数据结构比原有的stack多了翻转,和查询功能,

           查询功能 是从栈顶开始,向下做nand 运算,直到栈底;

发现 如果最后元素是0 ,

发现 如果最后元素是0 ,answer为0;

        可以想象成运算到离栈底最近的0后,answer为1,再接下来算剩下的1的answer;

#include
#include
#include
#include
#include
#include
using namespace std;
#define Size 200005
int dequee[Size*2],topp,bb,sizes;
struct bds
{
	int top,b;
	int flag,size;
	void inline init() { top=Size-1;b=Size;topp=top;bb=b;flag=1;size=0;sizes=0;} 
	void inline push(int x)
	{
	    if(flag) top++; else top--;
		if(!x) {
			if(flag) topp++,dequee[topp]=top;
			else bb--,dequee[bb]=top;
			sizes++;
		       } 
		size++;
	}
	int inline pop()
	{
		int x=0;
		if(flag) {if(dequee[topp]==top) x=1;top--;}
		if(!flag) { if(dequee[bb]==top) x=1;top++;}
		if(--size<0) return 1;
		if(x) {
			if(flag) topp--;
			else bb++; 
			sizes--;
		       }
		return 0;
	}
	int query()
	{
		int pos;
		if(size==0) return 3;
		if(size==1) return !sizes;
		if(sizes==0) return size&1;
		pos=dequee[(flag?bb:topp)];
		if(pos==b) return 1;
		if(flag&&postop) pos--;
		if(abs(b-pos)&1) return 1;
		return 0;
	}
	void inline reverse()
	{
		top=top+b;b=top-b;top=top-b;
		flag = !flag;
	}
};
int main()
{
	int t,q,kase=0,x;
	char qu[15];
	bds basic1;
	scanf("%d",&t);
	while(t--)
	{
		basic1.init();
		scanf("%d",&q);
		printf("Case #%d:\n",++kase);
		while(q--)
		{
		  scanf("%s",qu);
		  if(qu[0]=='P')
		  {
		  	 if(qu[1]=='U') { scanf("%d",&x); basic1.push(x);}
			 else basic1.pop();
		  }
		  if(qu[0]=='R') basic1.reverse();
		  if(qu[0]=='Q') 
		  {
		  	 int ans=basic1.query();
		  	 if(ans==3) printf("Invalid.\n");
		  	 else printf("%d\n",ans);
		  } 
		}
	}
}




你可能感兴趣的:(模拟,数据结构)