【HDU5929 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 H】【打表找规律 队列模拟】Basic Data Structure 双端栈下连续1和0做nand的结果
2016-10-08 16:46
435 查看
Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 516 Accepted Submission(s): 150
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.)
[align=left]Sample Input[/align]
2
8
PUSH 1
QUERY
PUSH 0
REVERSE
QUERY
POP
POP
#include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #include<set> #include<map> #include<vector> #include<queue> #include<bitset> #include<algorithm> #include<time.h> using namespace std; void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } #define MS(x,y) memset(x,y,sizeof(x)) #define ls o<<1 #define rs o<<1|1 typedef long long LL; typedef unsigned long long UL; typedef unsigned int UI; template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; } template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; } const int N = 4e5 + 10, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f; template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; } int casenum, casei; char op[10]; int val; int h, t; bool rev; int q ; int g ; int n; int main() { scanf("%d", &casenum); for (casei = 1; casei <= casenum; ++casei) { printf("Case #%d:\n", casei); h = 2e5 + 1; t = 2e5; rev = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%s", op); if (op[0] == 'P' && op[1] == 'U') { scanf("%d", &val); if (!rev) { if (h>t || q[h] != val) { q[--h] = val; g[h] = 1; } else ++g[h]; } else { if (h>t || q[t] != val) { q[++t] = val; g[t] = 1; } else ++g[t]; } } else if (op[0] == 'P' && op[1] == 'O') { if (h>t) { while (1); } if (!rev) { if (--g[h] == 0)++h; } else { if (--g[t] == 0)--t; } } else if (op[0] == 'R') { rev ^= 1; } else { if (!rev) { if (q[t] == 1) { int num = g[t]; if (h<t) { if (g[t - 1] > 1 || h + 1<t) { ++num; } } num %= 2; printf("%d\n", num); } else { int num = (g[t]>1 || h < t); printf("%d\n", num); } } else { if (q[h] == 1) { int num = g[h]; if (h<t) { if (g[h + 1] > 1 || h + 1<t) { ++num; } } num %= 2; printf("%d\n", num); } else { int num = (g[h]>1 || h < t); printf("%d\n", num); } } } } } return 0; } /* 【题意】 有一个栈,存在n(2e5)次操作。 对于每个操作,包括—— PUSH(x),x不是0就是1 POP() REVERSE() 对栈做从首到尾的翻转 QUERY() 从栈顶元素到栈底元素,做连续NAND操作并输出 0 nand 0 = 1 0 nand 1 = 1 1 nand 0 = 1 1 nand 1 = 0 【类型】 打表找规律 队列模拟 【分析】 这题的栈可以用队列模拟 关键如何快速知道nand这种运算的结果。 nand是不满足结合律的 于是打表找了个规律 发现,如果最后一个数是0,不管前面是什么,最后结果都是1 如果最后一个数不是0, 最后的连续1的个数如果是奇数,最后结果就是1 最后的连续1的个数如果是偶数,最后结果就是0 需要注意的是,这个连续1是要算上这串1之前的那个0的(因为这个0可能会生成1) 用队列模拟就做完啦! 【时间复杂度&&优化】 O(n) */
3PUSH 0REVERSEQUERY
[align=left]Sample Output[/align]
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.
[align=left]Source[/align]
2016CCPC东北地区大学生程序设计竞赛
- 重现赛
#include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #include<set> #include<map> #include<vector> #include<queue> #include<bitset> #include<algorithm> #include<time.h> using namespace std; void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } #define MS(x,y) memset(x,y,sizeof(x)) #define ls o<<1 #define rs o<<1|1 typedef long long LL; typedef unsigned long long UL; typedef unsigned int UI; template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; } template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; } const int N = 4e5 + 10, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f; template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; } int casenum, casei; char op[10]; int val; int h, t; bool rev; int q ; int g ; int n; int main() { scanf("%d", &casenum); for (casei = 1; casei <= casenum; ++casei) { printf("Case #%d:\n", casei); h = 2e5 + 1; t = 2e5; rev = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%s", op); if (op[0] == 'P' && op[1] == 'U') { scanf("%d", &val); if (!rev) { if (h>t || q[h] != val) { q[--h] = val; g[h] = 1; } else ++g[h]; } else { if (h>t || q[t] != val) { q[++t] = val; g[t] = 1; } else ++g[t]; } } else if (op[0] == 'P' && op[1] == 'O') { if (h>t) { while (1); } if (!rev) { if (--g[h] == 0)++h; } else { if (--g[t] == 0)--t; } } else if (op[0] == 'R') { rev ^= 1; } else { if (!rev) { if (q[t] == 1) { int num = g[t]; if (h<t) { if (g[t - 1] > 1 || h + 1<t) { ++num; } } num %= 2; printf("%d\n", num); } else { int num = (g[t]>1 || h < t); printf("%d\n", num); } } else { if (q[h] == 1) { int num = g[h]; if (h<t) { if (g[h + 1] > 1 || h + 1<t) { ++num; } } num %= 2; printf("%d\n", num); } else { int num = (g[h]>1 || h < t); printf("%d\n", num); } } } } } return 0; } /* 【题意】 有一个栈,存在n(2e5)次操作。 对于每个操作,包括—— PUSH(x),x不是0就是1 POP() REVERSE() 对栈做从首到尾的翻转 QUERY() 从栈顶元素到栈底元素,做连续NAND操作并输出 0 nand 0 = 1 0 nand 1 = 1 1 nand 0 = 1 1 nand 1 = 0 【类型】 打表找规律 队列模拟 【分析】 这题的栈可以用队列模拟 关键如何快速知道nand这种运算的结果。 nand是不满足结合律的 于是打表找了个规律 发现,如果最后一个数是0,不管前面是什么,最后结果都是1 如果最后一个数不是0, 最后的连续1的个数如果是奇数,最后结果就是1 最后的连续1的个数如果是偶数,最后结果就是0 需要注意的是,这个连续1是要算上这串1之前的那个0的(因为这个0可能会生成1) 用队列模拟就做完啦! 【时间复杂度&&优化】 O(n) */
相关文章推荐
- (HDU 5929)Basic Data Structure 双端队列+模拟 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- 2016CCPC东北地区大学生程序设计竞赛 H题 Basic Data Structure (模拟+双端队列)
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- hdu 5929 Basic Data Structure 2016CCPC东北地区大学生程序设计竞赛
- HDU Basic Data Structure 2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- (HDU 5922)Minimum’s Revenge 思维水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- (HDU 5924)Mr. Frog’s Problem 思维水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- 【HDU5922 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 A】【水题】Minimum’s Revenge
- HDU5929 Basic Data Structure,ccpc,模拟双端队列(两倍大的数组从中间开始向两旁拓展)
- 【HDU5923 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 B】【并查集 暴力 复杂度计算】Prediction 生效若干条链上的所有边条件下的联通块情况
- 【HDU5932 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 K】【树上背包 贪心乱搞】Backpack on Tree 物品成本只有12345下的树上背包
- 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 D Coconuts HDU 5925
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- 【HDU5924 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 C】【水题】Mr. Frog’s Problem
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- 【HDU5925 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 D】【离散化 BFS】Coconuts 大地图少数坏点输出每个联通块的大小
- (HDU 5926)Mr. Frog’s Game 水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >