hdu-5929 Basic Data Structure(双端队列+模拟)

题目链接:

Basic Data Structure

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

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 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 (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:
 
题意:
 
给一个栈.有push,pop,query ,reverse这些操作,对于每个询问输出这个栈从栈顶到底进行题目给的这个运算后的结果;
 
思路:
 
可以发现每次运算的结果跟到栈底最近的0下面有多少个1有关,所以双端队列里面维护的是0的位置,然后开一个2倍的数组模拟那个栈,每次翻转的时候就用flag 记录
是正向还是反向;然后就搞搞;我用数组模拟速度更快,而且比较和确定这个元素在当前情况下的相对位置也好确定一些;
 
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL;
typedef unsigned long long ULL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+4;
const int maxn=2e5+20;
const double eps=1e-12; int n,m,a[2*maxn];
char s[20];
deque<int>qu;
int flag,l,r;
void POP()
{
if(flag)
{
if(a[r]==0)qu.pop_back();
r--;
}
else
{
if(a[l]==0)qu.pop_front();
l++;
}
}
void PUSH(int x)
{
if(flag)
{
a[++r]=x;
if(x==0)qu.push_back(r);
}
else
{
a[--l]=x;
if(x==0)qu.push_front(l);
}
}
void Rev(){flag^=1;}
void query()
{
if(qu.empty())
{
if(r<l){printf("Invalid.\n");return ;}
int num=r-l+1;
if(num&1)printf("1\n");
else printf("0\n");
}
else
{
if(flag)
{
int fr=qu.front();
int num=fr-l;
if(num&1)
{
if(fr==r)printf("1\n");
else printf("0\n");
}
else
{
if(fr==r)printf("0\n");
else printf("1\n");
}
}
else
{
int fr=qu.back();
int num=r-fr;
if(num&1)
{
if(fr==l)printf("1\n");
else printf("0\n");
}
else
{
if(fr==l)printf("0\n");
else printf("1\n");
}
}
}
return ;
}
inline void Init()
{
flag=1;l=N;r=N-1;
while(!qu.empty())qu.pop_back();
}
int main()
{
int t,Case=0;
read(t);
while(t--)
{
Init();
printf("Case #%d:\n",++Case);
read(n);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
if(s[0]=='P')
{
if(s[1]=='U')
{
int x;
scanf("%d",&x);
PUSH(x);
}
else POP();
}
else if(s[0]=='R')Rev();
else query();
}
}
return 0;
}

  

 
上一篇:c#Winform程序,让pictureBox显示图像(包含GIF),并且不被占用,能即时删除图片。


下一篇:Java对象反序列化流