Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 406 Accepted Submission(s): 115
∙ 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.
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.
2 8 PUSH 1 QUERY PUSH 0 REVERSE QUERY POP POP QUERY 3 PUSH 0 REVERSE QUERY
Case #1: 1 1 Invalid. Case #2: 0HintIn 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 1或0,REVERSE操作为将栈倒序,QUERY操作询问从栈顶到栈底NAND的值,输出每次QUERY的结果,如果为空输出Invalid;
NAND这个运算其实就是和运算取非,而计算它的结果其实就是看栈顶到栈底最后一个0前后1的个数,因为如果0之前还有1,那么一定会和0变成1,如果0之前没有1,它就会和下个1结合:
1110 -> 1
11101 -> 0
0111-> 1
如果QUERY时每次都遍历询问会TLE,因此要使QUERY操作到达O(1),REVERSE操作也是(我用网上递归倒序栈的操作TLE了,据说是O(1)),因此考虑开一个双向队列,用flag标记当前的方向,对于每个操作根据不同的方向来确定队首还是队尾进行操作;
我这里双向队列记录的是从队首开始,每个0之后1的个数(默认队首有一个0,方便REVERSE操作后末尾为0的情况),用cnt0记录0的个数,用cnt1记录1未遇到0之前的个数;
用tot记录总数加减情况,因为开始向队列中放了0,所以直接用empty判断可能会出错;
<-front 100101 back->
PUSH:
如果遇到1,就让cnt1++,并且pop出上一个操作的cnt1,让当前的cnt1入队;如果遇到0,就让cnt0++,cnt1=0,并让0入队;
如果没有REVERSE就对入出front,反之入出back;
(因为计算的结果是从栈头到栈尾,所以非反向时是从back->front计算)
tot++;
POP:
如果遇到1,让cnt1--,再把原来的cnt1出队,当前cnt1入队;如果遇到0,就让cnt0--,并让0出队,把上个值赋给cnt1;
如果没有REVERSE就对入出front,反之入出back;
(因为0出队之后如果之前有1,那么下次操作时就要在原来1的个数上做加减)
tot--;
REVERSE:
flag取反,因为现在的队首值变为原来的队尾值,因此把1的个数赋给cnt1再进行操作;
QUERY:
其实就是考虑1110 1111这类情况的正反序;然后把最后一个0后1的个数进行判断奇偶即可;
最后进行清空队列即可~
#include <iostream>
#include <cstdio>
#include <deque>
#include <algorithm>
using namespace std;
const int N=200005;
char opr[10];
int num;
deque<int> d;
int main(){
int t,cas=1;
scanf("%d",&t);
while(t--){
int n;
int ans=0;
scanf("%d",&n);
printf("Case #%d:\n",cas++);
int tot=0;
int cnt0=0,cnt1=0;
int flag=0;//0=!reverse 1=reverse
d.push_back(0);
while(n--){
scanf("%s",opr);
if(opr[0]=='P'&&opr[1]=='U'){
scanf("%d",&num);
if(!flag){
if(num){
cnt1++;
d.pop_front();
d.push_front(cnt1);
}
else{
d.push_front(0);
cnt0++;
cnt1=0;
}
}
else{
if(num){
cnt1++;
d.pop_back();
d.push_back(cnt1);
}
else{
d.push_back(0);
cnt0++;
cnt1=0;
}
}
tot++;
}
else if(opr[0]=='P'&&opr[1]=='O'){
if(!flag){
if(d.front()){
cnt1--;
d.pop_front();
d.push_front(cnt1);
}
else{
d.pop_front();
cnt0--;
cnt1=d.front();
}
}
else{
if(d.back()){
cnt1--;
d.pop_back();
d.push_back(cnt1);
}
else{
d.pop_back();
cnt0--;
cnt1=d.back();
}
}
tot--;
}
else if(opr[0]=='R'){
if(!flag) cnt1=d.back();
else cnt1=d.front();
flag=!flag;
}
else if(opr[0]=='Q'){
if(tot==0){
printf("Invalid.\n");
}
else{
if(!flag){
if(cnt0==1&&d.front()==0){
ans=d.back()%2;
}
else if(cnt0==0){
ans=d.back()%2;
}
else{
ans=d.back()%2?0:1;
}
}
else{
if(cnt0==1&&d.back()==0){
ans=d.front()%2;
}
else if(cnt0==0){
ans=d.front()%2;
}
else{
ans=d.front()%2?0:1;
}
}
printf("%d\n",ans);
}
}
}
while(!d.empty()){
d.pop_back();
}
}
}
/*
2
8
PUSH 1
QUERY
PUSH 0
REVERSE
QUERY
POP
POP
QUERY
3
PUSH 0
REVERSE
QUERY
*/
/*
99
99
PUSH 0
PUSH 1
PUSH 1
PUSH 1
QUERY
POP
QUERY
REVERSE
QUERY
*/
之前用栈模拟然后就各种TLE。。
然后意思又理解错。。
尝试了数组和栈一起维护= =。。
不过最后用自己的方法过了~只用一个双向队列!~!~!~!~!~!~!~!~!
WA了好多次的截图……3点多才睡的我