当前位置: 代码迷 >> 综合 >> 【HDU - 5929】 Basic Data Structure 【双向链表+模拟】
  详细解决方案

【HDU - 5929】 Basic Data Structure 【双向链表+模拟】

热度:117   发布时间:2023-11-10 02:53:53.0

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,?,a1atop,atop?1,?,a1 is corresponding to the element of the Stack from top to the bottom, value=atopvalue=atop nand atop?1atop?1 nand … nand a1a1. 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≤20T≤20), which indicates the number of test cases.

For each test case, the first line contains only one integers N (2≤N≤2000002≤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.
题意 :
有一个栈,有四个操作:
push x : 将x插入栈顶(x=0 || x=1)
pop : 删除栈顶元素
reverse: 将栈所以元素反转
query:从栈顶到栈底所有数的nand值
让你输出每次查询的结果,若为空则输出Invalid

分析: 首先一看除了查询的操作,我们应该可以想到用到双向链表或者双向队列,所以关键问题就是如何的查询,看到数据范围我们能够想到肯定是有一定的规律的 ,0和所有数nand都为1,我们可以以这个为突破点,如果是这样的话,我们只要找到距离Top 最近的0的位置,因为还有反转操作,所以我们要得到距离Top最近的0的位置和最远的位置,这样我们可以用双向链表来维护所有的0的位置,之后就是模拟就好。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 5e5+11;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;int a[MAXN];
int main(){CLOSE();
// fread();
// fwrite();int T; scanf("%d",&T);int ncase=1;while(T--){printf("Case #%d:\n",ncase++);int q;list<int>L; memset(a,-1,sizeof(a));scanf("%d",&q);char op[10];int x;int flag=1;  // 1为正向 ,0为反向 int init = 2e5+11 ;  int left,right;left=init,right=init-1;while(q--){scanf("%s",op);if(op[0]=='P') {if(op[1]=='U') {    scanf("%d",&x);if(flag){a[++right]=x;if(x==0) L.push_front(right);}else {a[--left]=x;if(x==0) L.push_back(left);}}else {if(flag){if(a[right]==0) L.pop_front();--right;}else {if(a[left]==0) L.pop_back();++left; }}}else if(op[0]=='R') {flag^=1;}else if(op[0]=='Q') {if(right-left<0) {puts("Invalid.") ;continue;}if(L.empty())  {  // 这个时候说明全都为1if((right-left+1)&1) puts("1");else puts("0");} else {if(flag){int le=L.back();//得到反最左端的0的位置int a=le-left+((right-le)>0); //a为1的个数 if(a&1) puts("1");else puts("0");}else {int ri=L.front();int a=right-ri+((ri-left)>0) ;if(a&1) puts("1");else puts("0") ;}}}}}return 0;
}
  相关解决方案