双端队列+与非运算——Basic Data Structure ( HDU 5929 )
2016-10-07 11:07
567 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5929
分析:
实现一个数据结构,能够满足如下四种操作:
PUSH:把一个元素( 0 or 1 )放入栈的顶部。
POP:删除栈顶的一个元素( 只会在栈不为空的时候进行 )
REVERSE:翻转整个栈
QUERY:做操作 atopNANDatop−1NAND....NANDa1,然后输出结果,如果只有一个元素,直接输出该元素的值即可。
NAND运算:
![](http://img.blog.csdn.net/20161007105404300)
题解:
通过push和reverse操作我们可以想到用双向队列来实现这个数据结构。但是查询操作该怎么实现呢。我们可以发现与非运算的一个性质,那就是无论什么数与0进行与非运算都会得到1,那么问题就简单了,我们可以找到整个数据结构中最后一个0的位置,计算它后面有多少个1,然后根据奇偶性就能判断出结果。
注意:
我们使用set来存储0的位置的话,最后一个元素的访问应该用 *(–set.end()),STL 容器一般都是左闭右开的。
AC代码:
![](http://img.blog.csdn.net/20161007105710929)
http://acm.hdu.edu.cn/showproblem.php?pid=5929
分析:
实现一个数据结构,能够满足如下四种操作:
PUSH:把一个元素( 0 or 1 )放入栈的顶部。
POP:删除栈顶的一个元素( 只会在栈不为空的时候进行 )
REVERSE:翻转整个栈
QUERY:做操作 atopNANDatop−1NAND....NANDa1,然后输出结果,如果只有一个元素,直接输出该元素的值即可。
NAND运算:
题解:
通过push和reverse操作我们可以想到用双向队列来实现这个数据结构。但是查询操作该怎么实现呢。我们可以发现与非运算的一个性质,那就是无论什么数与0进行与非运算都会得到1,那么问题就简单了,我们可以找到整个数据结构中最后一个0的位置,计算它后面有多少个1,然后根据奇偶性就能判断出结果。
注意:
我们使用set来存储0的位置的话,最后一个元素的访问应该用 *(–set.end()),STL 容器一般都是左闭右开的。
AC代码:
/************************************************************************* > File Name: 1008.cpp > Author: Akira > Mail: qaq.febr2.qaq@gmail.com > Created Time: 2016年10月06日 星期四 13时00分08秒 ************************************************************************/ #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cmath> #include <vector> #include <set> #include <list> #include <ctime> typedef long long LL; typedef unsigned long long ULL; typedef long double LD; #define MST(a,b) memset(a,b,sizeof(a)) #define CLR(a) MST(a,0) #define Sqr(a) ((a)*(a)) using namespace std; #define MaxN 500000 #define MaxM MaxN*10 #define INF 0x3f3f3f3f #define bug cout<<88888888<<endl; int T; int n; int head; int tail; char op[10]; int main() { scanf("%d", &T); for(int t=1;t<=T;t++) { int Min=INF; int Max=0; int cnt = 0; set<int> s; set<int>::iterator it; int A[MaxN]; cout << "Case #" << t <<":\n"; head = tail = 250000; scanf("%d", &n); while(n--) { scanf("%s", op); if(op[0] == 'P') { if(op[1] == 'U') { int x; scanf("%d", &x); if(x==0) { s.insert(tail); //cout << tail << endl; } if(tail>=head) A[tail++] = x; else A[tail--] = x; } else { if( head > tail ) tail++; else tail--; if(A[tail] == 0) { s.erase(tail); //cout << tail << endl; } } } else if(op[0] == 'R') { if(head > tail) { int tmp = head; head = tail+1; tail = tmp +1; } else if( head < tail ) { int tmp = head; head = tail-1; tail = tmp -1; } else{ continue; } } else { if(head == tail) { cout << "Invalid.\n"; } else { if(abs(head-tail)==1) { cout << A[head] << endl; } else { int index; if( head>tail ) { if(!s.empty()) { if(s.size() == 1 && *s.begin() == tail+1) { index = tail+2; } else index = *(--s.end()); } else index = tail+1; } else { if(!s.empty()) { if( s.size() == 1 && *s.begin() == tail-1 ) { index = tail-2; } else index = (*s.begin()); } else index = tail-1; } int num = abs(head-index)+1; if(num&1) cout << 1 << endl; else cout << 0 << endl; } } } } } }
相关文章推荐
- hdu 5929 CCPC东北四省赛H - Basic Data Structure
- hdu 5929 Basic Data Structure(双端队列+模拟)
- Basic Data Structure hdu 5929
- HDU - 5929 Basic Data Structure(模拟+规律)
- hdu 5929 Basic Data Structure(找规律+deque模拟)
- hdu 5929 Basic Data Structure
- HDU-5929 Basic Data Structure
- HDU 5929 Basic Data Structure(deque双向队列+找规律)
- Basic Data Structure HDU 5929(模拟)
- hdu-5929 Basic Data Structure(双端队列+模拟)
- Basic Data Structure(HDU 5929 2016东北地区CCPC)
- HDU 5929 Basic Data Structure(模拟,deque)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- 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
- H - Basic Data Structure HDU - 5929
- HDU Basic Data Structure 2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- hdoj 5927 Auxiliary Set && hdoj 5929 Basic Data Structure