Basic Data Structurek(模拟,双向队列)
来源:互联网 发布:.club域名价值 编辑:程序博客网 时间:2024/05/18 16:40
Basic Data Structure
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 260 Accepted Submission(s): 62
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 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.
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.
可以用双向队列模拟,压入队列的数字代表连续1的个数,0的话直接压入。具体见代码,国足又输了55555555555.
#include<iostream>#include<string.h>#include<deque>#include<cstdio>using namespace std;int main(){ int cas; scanf("%d",&cas); int nn=0; while(cas--) { deque<int>ideq; printf("Case #%d:\n",++nn); int n; scanf("%d",&n); char cmd[105]; int num; int flag=0; int cc=0; for(int i=0;i<n;i++) { scanf("%s",&cmd); if(cmd[2]=='S') { cin>>num; if(ideq.empty()) { ideq.push_front(num); continue; } if(flag==0) { if(num==1) { cc=ideq.back(); if(cc==0) { ideq.push_back(1); continue; } ideq.pop_back(); cc++; ideq.push_back(cc); } else ideq.push_back(0); } else { if(num==1) { cc=ideq.front(); if(cc==0) { ideq.push_front(1); continue; } cc++; ideq.pop_front(); ideq.push_front(cc); } else ideq.push_front(0); } } else if(cmd[0]=='Q') { int n; if(flag==1&&!ideq.empty()) { int ans; int nnn=ideq.back(); ans=nnn; ideq.pop_back(); if(nnn==0) { if(!ideq.empty()) { printf("1\n"); } else { printf("0\n"); } ideq.push_back(nnn); continue; } if(!ideq.empty()) { int tmp=ideq.back(); ideq.pop_back(); if((tmp==0&&!ideq.empty())) ans++; ideq.push_back(tmp); } ideq.push_back(nnn); printf("%d\n",ans&1); } else if(flag==0&&!ideq.empty()) { int ans; int nnn=ideq.front(); ans=nnn; ideq.pop_front(); if(nnn==0) { if(!ideq.empty()) { printf("1\n"); } else { printf("0\n"); } ideq.push_front(nnn); continue; } if(!ideq.empty()) { int tmp=ideq.front(); ideq.pop_front(); if((tmp==0&&!ideq.empty())) ans++; ideq.push_front(tmp); } ideq.push_front(nnn); printf("%d\n",ans&1); } else { printf("Invalid.\n"); } } else if(cmd[1]=='O') { if(ideq.empty()) { continue; } if(flag==0) { int t=ideq.back(); ideq.pop_back(); if(t) { t--; if(t) ideq.push_back(t); } } else { int t=ideq.front(); ideq.pop_front(); if(t) { t--; if(t) ideq.push_front(t); } } } else if(cmd[0]=='R') { flag=!flag; } } } //system("pause");}
0 0
- Basic Data Structurek(模拟,双向队列)
- HDU 5929 Basic Data Structure(deque双向队列+找规律)
- 2016CCPC东北地区大学生程序设计竞赛 H题 Basic Data Structure (模拟+双端队列)
- hdu 5929 Basic Data Structure(双端队列+模拟)
- HDU 4286 Data Handler( 数据结构,双向队列,模拟)
- HDU 5929 Basic Data Structure (模拟)
- UVA210 concurrency simulator (双向队列 & 模拟)
- HDU5929 Basic Data Structure,ccpc,模拟双端队列(两倍大的数组从中间开始向两旁拓展)
- (HDU 5929)Basic Data Structure 双端队列+模拟 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- hdu 5929 Basic Data Structure (双端队列)
- HDU 5929 Basic Data Structure(双端队列)
- hdu 5929 Basic Data Structure(找规律,模拟)
- hdu 5929 Basic Data Structure(找规律+deque模拟)
- 双向队列(队列)
- HDU5929 Basic Data Structure(双端队列)
- hdu 5929 Basic Data Structure 模拟
- hdu5929 Basic Data Structure 规律+模拟
- HDU - 5929 Basic Data Structure(模拟+规律)
- OPENCV 图像拼接 stitching的使用
- PUT or POST in RESTful API design
- centos7 LAMP 加装 phpmyadmin (mariadb)
- GarsiaWachs算法:石子归并问题
- awk+system+getline
- Basic Data Structurek(模拟,双向队列)
- C++复习
- 秋天SWF生成PDF免费版V1.4
- hdu 5929 Basic Data Structure 双端队列
- 剑指Offer 3题 二维数组中的查找 Java
- 选择想要出现的图片张数
- Jekyll和Github搭建个人静态博客
- Linux内存寻址之一:内存地址分类以及MMU介绍
- Java集合——TreeMap源码详解