您的位置:首页 > 其它

HDU 5929 Basic Data Structure(双端队列)

2017-11-08 23:08 621 查看


Basic Data Structure

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2849    Accepted Submission(s): 628


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.

 

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.

 

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.

 

Source

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

 

题意:
给你一个栈,让你进行如上四个操作。

POINT:
反转不需要真正反转,只要标记一下那边变成了Top就行了。
计算的时候,只要从栈顶往下找到最后一个0,然后在计算下面有多少个1就行了。
注意栈顶是就是最后一个0的情况,那么这个0和下一个1组合变为1.

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <string.h>
#include <map>
using namespace std;
//#define LL long long
const int maxn = 300000*2;
int a[maxn];

int main()
{
int T;
int cas=1;
deque<int>q;
for(scanf("%d",&T);T--;cas++){
q.clear();
int n;scanf("%d",&n);
int now=1;
int l=300001,r=300000;
printf("Case #%d:\n",cas);
for(int i=1;i<=n;i++){
char s[10];
scanf("%s",s);
if(!strcmp(s, "PUSH")){
int x;scanf("%d",&x);
if(now){
++r;
if(!x) q.push_back(r);
a[r]=x;
}else{
--l;
if(!x) q.push_front(l);
a[l]=x;
}
}else if(!strcmp(s, "POP")){
if(now){
if(!a[r]){
q.pop_back();
}
r--;
}else{
if(!a[l]){
q.pop_front();
}
l++;
}
}else if(!strcmp(s, "REVERSE")){
now=now^1;
}else{
if(l>r) printf("Invalid.\n");
else if(now){
if(q.empty()){
printf("%d\n",(r-l+1)%2);
continue;
}
if(q.front()==r){
printf("%d\n",(r-l)%2);
}else{
printf("%d\n",(q.front()-l+1)%2);
}
}else{
if(q.empty()){
printf("%d\n",(r-l+1)%2);
continue;
}
if(q.back()==l){
printf("%d\n",(r-l)%2);
}else{
printf("%d\n",(r-q.back()+1)%2);
}
}
}
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: