2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟

Basic Data Structure

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

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.

 
题意:维护一个栈,支持往栈里塞 0/1 ,弹栈顶,翻转栈,询问从栈顶到栈底按顺序 NAND 的值。
题解:只要知道最后的 0后面 1的个数的奇偶性就行。
我这里是用set 存储0的位置 比较麻烦 也可以和存储0/1一样 维护一个 0的位置 的栈
感谢评论区 hack数据
1
10
PUSH 0
REVERSE
QUERY
PUSH 1
PUSH 1
REVERSE
POP
QUERY
POP
QUERY

  代码中已经标记更改

 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
//typedef long long ll;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int t;
char str[];
map<int,int> mp;
int l,r;
int l0,r0;
int exm;
int flag=;
int n;
set<int>se;
set<int>::iterator it;
int biao=;
int main()
{
scanf("%d",&t);
{
for(int k=; k<=t; k++)
{
se.clear();
mp.clear();
biao=;
scanf("%d",&n);
l=r=;
printf("Case #%d:\n",k);
for(int i=; i<=n; i++)
{
scanf("%s",str);
if(strcmp(str,"PUSH")==)
{
scanf("%d",&exm);
if(exm==)
se.insert(l); mp[l]=exm;
if(biao==)
l++;
else
l--;
}
if(strcmp(str,"POP")==)
{
if(biao==)
l--;
else
l++;
}
if(strcmp(str,"REVERSE")==)
{
if(biao==)
{
l--;
r--;
swap(l,r);
biao=;
}
else
{
l++;
r++;
swap(l,r);
biao=;
}
}
if(strcmp(str,"QUERY")==)
{
if(l==r)
printf("Invalid.\n");
else
{
if(biao==)
{
int exm;
int gg=;
for(it=se.begin(); it!=se.end(); it++)
{
if(*it>=r)
{
exm=*it;
gg=;
break;
}
}
if(exm>=l)/*评论区hack点 更正 */
gg=;
if(gg==)
{
if((l-r)%==)
printf("0\n");
else
printf("1\n");
}
else
{
if(l==r+)
printf("%d\n",mp[exm]);
else
{
if(exm==(l-))
exm--;
if((exm-r+)%)
printf("1\n");
else
printf("0\n");
}
}
}
else
{
int exm;
int gg=;
if(se.size()!=)
{
it=--se.end();
for(;; it--)
{
if(*it<=r)
{
exm=*it;
gg=;
break;
}
if(it==se.begin())
break;
}
}
if(exm<=l)/*评论区hack点 更正*/
gg=;
if(gg==)
{
if((r-l)%==)
printf("0\n");
else
printf("1\n");
}
else
{
int hhh=;
if(l==r-)
printf("%d\n",mp[exm]);
else
{
if(exm==(l+))
exm++;
if((r-exm+)%)
printf("1\n");
else
printf("0\n");
}
}
}
}
}
}
}
}
return ;
}
/*
2
6
PUSH 1
PUSH 1
PUSH 1
PUSH 1
REVERSE
QUERY
5
PUSH 1
PUSH 1
PUSH 1
PUSH 1
QUERY
*/
上一篇:vmware通过vmnet8共享本地网络


下一篇:[转载]Meta Learning单排小教学