Basic Data Structure HDU 5929(模拟)
来源:互联网 发布:注塑机生产厂家 知乎 编辑:程序博客网 时间:2024/05/21 22:34
Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)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 doNAND 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.
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:
By the way, NAND is a basic binary operation:
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.
For each test case, the first line contains only one integers N (
In the following N lines, the i-th line contains one of these operations below:
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
28PUSH 1QUERYPUSH 0REVERSEQUERYPOPPOPQUERY3PUSH 0REVERSEQUERY
Sample Output
Case #1:11Invalid.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 pop
后两个比较重要 1是栈翻转操作 2是询问操作
首先1 我的做法是用数组模拟 (体会到了数据结构的重要性) top bottom 记录临时的头尾 比较大小就可以得知谁是头尾
其次2 这是本t的重点难点 遍历一遍肯定超时 我们可以看到与非逻辑关系 只要有0不管和谁运算都出现1 利用这个性质记录栈中最后出栈的0的位置
例如 top11011110bottom 因为栈底是0 那么答案就是1 top1100111011bottom 判断0后面出现1的奇偶性就可
又例如 top01111bottom
还有 top0000bottom 以及top1111bottom top0bottom 这几种特殊情况
#include <algorithm>#include <iostream>#include <cstring>#include <iomanip>#include <string>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <map>using namespace std;#define For(i,a,b) for(i=a;i<=b;i++)#define _For(i,a,b) for(i=b;i>=a;i--)#define Out(x) cout<<x<<endl#define Outdouble(x,a) cout<<fixed<<setprecision(a)<<1.0*x<<endl#define pf printf#define sf scanf#define mset(arr,num) memset(arr,num,sizeof(arr))#define ll long longconst ll inf = 800010; ///#define ok std::ios::sync_with_stdio(0)#pragma comment(linker, "/STACK:102400000,102400000")// #define debug#if defined (debug)---check---#endif/// ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ///const int start = 300000;bool arr[inf];int bottom = start;int top = bottom + 1;int oddsum = 0;int sum;deque <int> q;void Push(int y){ sum++; bool x = (bool)y; if(top < bottom) { arr[top] = x; if(x == 0) { q.push_front(top); ///left } top--; } else { arr[top] = x; if(x == 0) { q.push_back(top); ///right } top++; }}void Reve(){ swap(top,bottom);}void Que(){ int num; if(abs(top-bottom) == 1) { pf("Invalid.\n"); ///empty return ; } if(q.size() == 0) ///all of deque is 1 { pf("%d\n",sum%2); return ; } if(top < bottom) { if(q.back() == bottom - 1) ///序列最后(back)一个是0那么答案是1/0 { if(sum > 1) { pf("1\n"); } else if(sum == 1) { pf("0\n"); } return ; } num = bottom - 1 - q.back(); if(q.back() == top + 1) ///如果back是top元素说明前面无1 奇1偶0 { if(num%2 == 1) { pf("1\n"); } else pf("0\n"); } else { if(num%2 == 1) { pf("0\n"); } else pf("1\n"); } } else ///top > bottom { if(q.front() == bottom + 1) ///最前(front)一个是0那么答案是1 { if(sum > 1) { pf("1\n"); } else if(sum == 1) { pf("0\n"); } return ; } num = q.front() - bottom -1; ///--17:18 if(q.front() == top - 1) ///如果front是top元素说明前面无1 奇1偶0 { if(num%2 == 1) { pf("1\n"); } else pf("0\n"); } else { if(num%2 == 1) { pf("0\n"); } else pf("1\n"); } }}void Pop(){ sum--; if(top < bottom) { top++; if(arr[top] == 0) { q.pop_front(); } } else { top--; if(arr[top] == 0) { q.pop_back(); } }}int main(){ int i,cnt = 0;; int t,n; char s[20]; sf("%d",&t); while(t--) { bottom = start; top = bottom + 1; sum = 0; cnt++; q.clear(); sf("%d",&n); pf("Case #%d:\n",cnt); For(i,1,n) { sf("%s",s); if(s[0] == 'P' && s[1] == 'U') { int x; cin>>x; Push(x); } else if(s[0] == 'P' && s[1] == 'O') { Pop(); } else if(s[0] == 'R') { Reve(); } else if(s[0] == 'Q') { Que(); } } } return 0;}
1 0
- hdu 5929 Basic Data Structure 模拟
- HDU 5929 Basic Data Structure (模拟)
- HDU - 5929 Basic Data Structure(模拟+规律)
- Basic Data Structure HDU 5929(模拟)
- hdu 5929 Basic Data Structure(找规律,模拟)
- hdu 5929 Basic Data Structure(双端队列+模拟)
- hdu 5929 Basic Data Structure 找规律 模拟
- HDU 5929 Basic Data Structure(模拟栈+规律)
- hdu 5929 Basic Data Structure(找规律+deque模拟)
- hdu 5929 Basic Data Structure
- Basic Data Structure hdu 5929
- HDU-5929 Basic Data Structure
- HDU 5929Basic Data Structure
- [HDU 5929] Basic Data Structure (脑洞题)
- hdu 5929 Basic Data Structure(山东CCPC)
- H - Basic Data Structure HDU - 5929
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5292 Basic Data Structure
- JavaWeb中遇到的坑和解决方案
- java 木马开发(7)---服务端---锁住鼠标函数 mouseLock 和 闪屏函数 Flash
- OpenCV人脸识别程序
- mysql5.7.9详细安装教程 mac10.10
- LoadLibray失败,返回错误码193
- Basic Data Structure HDU 5929(模拟)
- Tensorflow并行计算:多核(multicore),多线程(multi-thread),图分割(Graph Partition)
- 算法训练 P1103
- 有任何问题或建议可以私信给我们
- fragment之间传递数据
- [华为OJ--C++]041查找输入整数二进制中1的个数
- Cookie和Session之间的关系重新复习一下
- Java中的运算符
- 第24天(就业班) 餐馆王项目需求(一)