首页 > 代码库 > 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 1008(hdu 5929)
2016CCPC东北地区大学生程序设计竞赛 - 重现赛 1008(hdu 5929)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5929
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,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.
? 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.
Input
The first line contains only one integer T (T≤20), which indicates the number of test cases.
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.
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.
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:
题意:
要求模拟一个存入0,1的栈,有4个操作
1:push a,在栈顶插入a。
2:pop,删除栈顶。
3:reverse,将这个栈翻转一下(注意:这里一旦换位置,其输入的位置也要换的)。
4:query,询问栈顶到栈底的atop nand atop-1 nand ... a1。
题解:
我自己是用数组模拟双端队列的方式来做的。
其实其他的几个步骤都是比较简单实现的。就是求和的时候比较坑,常规的方法是不行的,预处理也不行。但是通过分析数据可以找到一个比较明显的规律,直接找离最后位置最近的0的位置,然后再找0之后1的个数,个数奇值为1,个数偶为值0,就行了。
代码:
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;#define inf 0x3f3f3f3fconst int maxn = 2*1e5;#define met(a,b) memset(a,b,sizeof(a))char s[4][10]={"PUSH","POP","REVERSE","QUERY"};ll num[2*maxn];int main(){ int t; ll k=1; scanf("%d",&t); while(t--) { ll n; scanf("%lld",&n); printf("Case #%lld:\n",k++); ll len=0; ll flag=1; ll num1=-inf,num2=inf; ll l=maxn,r=maxn-1; for(ll i=0;i<n;i++) { char s1[10]; scanf("%s",s1); if(strcmp(s[0],s1)==0) { ll x,pos; scanf("%lld",&x); if(flag) { num[++r]=x; pos=r; } else { num[--l]=x; pos=l; } len++; if(x==0) { num1=max(num1,pos); num2=min(num2,pos); } } if(strcmp(s[1],s1)==0) { if(flag) r--; else l++; len--; } if(strcmp(s[2],s1)==0) { flag^=1; } if(strcmp(s[3],s1)==0) { if(len<=0) { printf("Invalid.\n"); continue; } else { if(num1==-inf&&num2==inf) { ll len=r-l+1; if(len&1) printf("1\n"); else printf("0\n"); continue; } if(flag) { ll ling=min(num1,num2); ll len=ling-l+1; if(ling==r) len--; if(len&1) printf("1\n"); else printf("0\n"); } else { ll ling=max(num1,num2); ll len=r-ling+1; if(ling==l) len--; if(len&1) printf("1\n"); else printf("0\n"); } } } } }}
2016CCPC东北地区大学生程序设计竞赛 - 重现赛 1008(hdu 5929)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。