HDU

来源:互联网 发布:json key 数字 编辑:程序博客网 时间:2024/06/04 18:32

Basic Data Structure

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2844    Accepted Submission(s): 626


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 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 (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
28PUSH 1QUERYPUSH 0REVERSEQUERYPOPPOPQUERY3PUSH 0REVERSEQUERY
 

Sample Output
Case #1:11Invalid.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.
 

Source
2016CCPC东北地区大学生程序设计竞赛 - 重现赛 
 


题意:有一种栈可以完成以下四种操作

  1. "PUSH x" 将 x 放到栈顶,x只能为 1 或 2
  2. "POP" 将栈顶元素推出
  3. "QUERY" 询问从栈顶到栈底所有元素依次进行NAND操作后的结果,栈为空时输出 Invalid.
  4. "REVERSE" 将栈翻转,即原来栈顶元素变为栈底元素,从上往下第二个元素变为从下往上第二个元素。。。

NAND操作: 

 0 nand 0 = 1
 0 nand 1 = 1
 1 nand 0 = 1
 1 nand 1 = 0


思路:

从栈顶往下到最后一个0之间的元素可以忽略,因为1与0和0进行NAND操作得到的结果一定为1,然后再计算最后一个零之后有几个1,记为a,就可以看作是有a+1个1(当最后一个0在栈顶时只能看做是a个1),1的个数为偶数答案为0,奇数为1。

只用记录从下往上第一个0和最后一个0的位置就可以了,翻转前取最后一个,翻转后取第一个。


#include <iostream>#include <stdio.h>#include <string.h>#include <deque>using namespace std;const int maxn = 2e5 +10;int T,n,x,cas = 0,st[maxn*3];char s[10];int main(){    scanf("%d",&T);    while(T--){        scanf("%d",&n);        printf("Case #%d:\n",++cas);        deque<int>q;q.clear();        int l = maxn, r = maxn - 1, now = 0;        while(n--){            scanf("%s",s);            if(!strcmp(s,"PUSH")){                scanf("%d",&x);                if(!now) st[--l] = x;                else st[++r] = x;                if(x==0){                    if(!now) q.push_front(l);                    else q.push_back(r);                }            }else if(!strcmp(s, "POP")){                if(!now){                    if(!st[l]) q.pop_front();                    ++l;                }else{                    if(!st[r]) q.pop_back();                    --r;                }            }else if(!strcmp(s, "QUERY")){                if(r<l) {printf("Invalid.\n");continue;}                if(q.empty()) {printf("%d\n",(r-l+1)%2);continue;}                if(!now){                    if(q.back()==l) printf("%d\n",(r-l)%2);                    else printf("%d\n",(r-q.back()+1)%2);                }else{                    if(q.front()==r) printf("%d\n",(r-l)%2);                    else printf("%d\n",(q.front()-l+1)%2);                }                            }else{                now^=1;            }        }    }    return 0;}



原创粉丝点击