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的位置 的栈

  1 /******************************
  2 code by drizzle
  3 blog: www.cnblogs.com/hsd-/
  4 ^ ^    ^ ^
  5  O      O
  6 ******************************/
  7 #include<bits/stdc++.h>
  8 #include<map>
  9 #include<set>
 10 #include<cmath>
 11 #include<queue>
 12 #include<bitset>
 13 #include<math.h>
 14 #include<vector>
 15 #include<string>
 16 #include<stdio.h>
 17 #include<cstring>
 18 #include<iostream>
 19 #include<algorithm>
 20 #pragma comment(linker, "/STACK:102400000,102400000")
 21 using namespace std;
 22 #define  A first
 23 #define B second
 24 const int mod=1000000007;
 25 const int MOD1=1000000007;
 26 const int MOD2=1000000009;
 27 const double EPS=0.00000001;
 28 //typedef long long ll;
 29 typedef __int64 ll;
 30 const ll MOD=1000000007;
 31 const int INF=1000000010;
 32 const ll MAX=1ll<<55;
 33 const double eps=1e-5;
 34 const double inf=~0u>>1;
 35 const double pi=acos(-1.0);
 36 typedef double db;
 37 typedef unsigned int uint;
 38 typedef unsigned long long ull;
 39 int t;
 40 char str[10];
 41 map<int,int> mp;
 42 int l,r;
 43 int l0,r0;
 44 int exm;
 45 int flag=0;
 46 int n;
 47 set<int>se;
 48 set<int>::iterator it;
 49 int biao=0;
 50 int main()
 51 {
 52     scanf("%d",&t);
 53     {
 54         for(int k=1; k<=t; k++)
 55         {
 56             se.clear();
 57             mp.clear();
 58             biao=0;
 59             scanf("%d",&n);
 60             l=r=0;
 61             printf("Case #%d:\n",k);
 62             for(int i=1; i<=n; i++)
 63             {
 64                 scanf("%s",str);
 65                 if(strcmp(str,"PUSH")==0)
 66                 {
 67                     scanf("%d",&exm);
 68                     if(exm==0)
 69                         se.insert(l);
 70
 71                     mp[l]=exm;
 72                     if(biao==0)
 73                         l++;
 74                     else
 75                         l--;
 76                 }
 77                 if(strcmp(str,"POP")==0)
 78                 {
 79                     if(biao==0)
 80                         l--;
 81                     else
 82                         l++;
 83                 }
 84                 if(strcmp(str,"REVERSE")==0)
 85                 {
 86                     if(biao==0)
 87                     {
 88                         l--;
 89                         r--;
 90                         swap(l,r);
 91                         biao=1;
 92                     }
 93                     else
 94                     {
 95                         l++;
 96                         r++;
 97                         swap(l,r);
 98                         biao=0;
 99                     }
100                 }
101                 if(strcmp(str,"QUERY")==0)
102                 {
103                     if(l==r)
104                         printf("Invalid.\n");
105                     else
106                     {
107                         if(biao==0)
108                         {
109                             int exm;
110                             int gg=0;
111                             for(it=se.begin(); it!=se.end(); it++)
112                             {
113                                 if(*it>=r)
114                                 {
115                                     exm=*it;
116                                     gg=1;
117                                     break;
118                                 }
119                             }
120                             if(gg==0)
121                             {
122                                 if((l-r)%2==0)
123                                     printf("0\n");
124                                 else
125                                     printf("1\n");
126                             }
127                             else
128                             {
129                                 if(l==r+1)
130                                     printf("%d\n",mp[exm]);
131                                 else
132                                 {
133                                     if(exm==(l-1))
134                                         exm--;
135                                     if((exm-r+1)%2)
136                                         printf("1\n");
137                                     else
138                                         printf("0\n");
139                                 }
140                             }
141                         }
142                         else
143                         {
144                             int exm;
145                             int gg=0;
146                             if(se.size()!=0)
147                             {
148                                 it=--se.end();
149                                 for(;; it--)
150                                 {
151                                     if(*it<=r)
152                                     {
153                                         exm=*it;
154                                         gg=1;
155                                         break;
156                                     }
157                                     if(it==se.begin())
158                                         break;
159                                 }
160                             }
161                             if(gg==0)
162                             {
163                                 if((r-l)%2==0)
164                                     printf("0\n");
165                                 else
166                                     printf("1\n");
167                             }
168                             else
169                             {
170                                 int hhh=1;
171                                 if(l==r-1)
172                                     printf("%d\n",mp[exm]);
173                                 else
174                                 {
175                                     if(exm==(l+1))
176                                         exm++;
177                                     if((r-exm+1)%2)
178                                         printf("1\n");
179                                     else
180                                         printf("0\n");
181                                 }
182                             }
183                         }
184                     }
185                 }
186             }
187         }
188     }
189     return 0;
190 }
191 /*
192 2
193 6
194 PUSH 1
195 PUSH 1
196 PUSH 1
197 PUSH 1
198 REVERSE
199 QUERY
200 5
201 PUSH 1
202 PUSH 1
203 PUSH 1
204 PUSH 1
205 QUERY
206 */
时间: 2024-12-12 12:46:56

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

HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 524    Accepted Submission(s): 151 Problem Description TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams abou

HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 312    Accepted Submission(s): 219 Problem Description One day, you, a clever boy, feel bored in your math class, and then fall

HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)

Auxiliary Set Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 873    Accepted Submission(s): 271 Problem Description Given a rooted tree with n vertices, some of the vertices are important. An a

HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 283    Accepted Submission(s): 219 Problem Description There is a graph of n vertices which are indexed from 1 to n. For any pai

2016CCPC东北地区大学生程序设计竞赛 - 重现赛 1008(hdu 5929)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5929 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

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

链接http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:给你一种数据结构以及操作,和一种位运算,最后询问:从‘栈’顶到低的运算顺序结果是多少 解法:根据位运算,发现出现0,结果就是1,那么就记录两端0的位置就好,中间不管出现什么,结果大部分都是1,考虑还有反转操作,使用双端队列,用flag标记反转后的情况,然后根据需要添加元素记录位置,最后根据标记,出现元素等进行讨论计算 #include <iostream> #include <dequ

2016CCPC东北地区大学生程序设计竞赛 (2018年8月22日组队训练赛)

题目链接:http://acm.hdu.edu.cn/search.php?field=problem&key=2016CCPC%B6%AB%B1%B1%B5%D8%C7%F8%B4%F3%D1%A7%C9%FA%B3%CC%D0%F2%C9%E8%BC%C6%BE%BA%C8%FC+-+%D6%D8%CF%D6%C8%FC&source=1&searchmode=source A题题目: 题意: 生成一棵最小生成树,边权值为两个节点的最小公倍数. 思路: 由最小公倍数的和最大公约

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

链接http://acm.hdu.edu.cn/showproblem.php?pid=5924 题意:根据公式求C,D 解法:打表找规律 #include <bits/stdc++.h> using namespace std; #define ll long long int main() { int t,cnt=1; scanf("%d",&t); while(t--) { ll a,b; scanf("%I64d%I64d",&a

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

链接http://acm.hdu.edu.cn/showproblem.php?pid=5926 题意:给我们一个矩阵,问你根据连连看的玩法可以消去其中的元素 解法:连连看怎么玩,就怎么写,别忘记边界 #include<stdio.h> //#include<bits/stdc++.h> #include<string.h> #include<iostream> #include<math.h> #include<sstream> #