H - Basic Data Structure HDU - 5929
来源:互联网 发布:卡巴斯基网络防火墙 编辑:程序博客网 时间:2024/06/05 23:48
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.
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.
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.
28PUSH 1QUERYPUSH 0REVERSEQUERYPOPPOPQUERY3PUSH 0REVERSEQUERY
Case #1:11Invalid.Case #2:0
思路:双向队列存储0的位置。因为不管0前面的值为多少,碰到0就会变成1.然后看一下剩余的1的个数就可以了。
坑点:
1.双向队列首先要判空。直接算1的个数就可以了。
2.0前面没有值得情况。这时候0的1不计算。
#include <bits/stdc++.h>using namespace std;const int MAXN=4e5+7;const int inf=1e9;char s[10];int num[MAXN];int main(){ int i; int t; int n; int l,r; scanf("%d",&t); for(int tt=1; tt<=t; ++tt) { deque<int>q; scanf("%d",&n); printf("Case #%d:\n",tt); int x; l=r=200001; int flag=0; for(i=0; i<n; ++i) { scanf("%s",s); if(s[0]=='P') { if(s[1]=='U') { scanf("%d",&x); if(!flag) { num[r++]=x; if(!x)q.push_back(r-1); } else { num[--l]=x; if(!x)q.push_front(l); } } else { if(!flag) { r--; if(!num[r])q.pop_back(); } else { if(!num[l])q.pop_front(); l++; } } } else if(s[0]=='R')flag=(flag+1)%2; else if(s[0]=='Q') { if(l>=r)puts("Invalid."); else if(q.empty())printf("%d\n",(r-l)%2); else { int ans; if(r-l==1)printf("%d\n",num[l]); else { if(!flag) { ans=(q.front()-l+(q.front()!=r-1))%2; } else { ans=(r-q.back()-1+(q.back()!=l))%2; } printf("%d\n",ans); } } } } } return 0;}
0 0
- H - Basic Data Structure HDU - 5929
- hdu 5929 CCPC东北四省赛H - Basic Data Structure
- 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)
- hdu 5929 Basic Data Structure 模拟
- HDU 5929 Basic Data Structure (模拟)
- HDU - 5929 Basic Data Structure(模拟+规律)
- Basic Data Structure HDU 5929(模拟)
- HDU 5292 Basic Data Structure
- 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 找规律 模拟
- 序列化的作用
- 构建自动化前端样式回归测试——BackstopJS篇
- 算法训练 出现次数最多的整数(Hashtable)
- iOS调试常见错误 如:symbol(s) not found for architecture x86_64
- 安卓开发自定义View
- H - Basic Data Structure HDU - 5929
- idea cannot resolve symbol
- 自定义TextView实现设置drawable图片大小
- 关于WNDCLASSEX窗口类
- lab颜色空间
- SQL 测试用
- 2. Add Two Numbers
- Android通过反射的方法获取手机存储设备的状态信息
- linux 时钟定时器