数据结构 ( 优先队列&&栈 )——HDU 5818 ( 2016 Multi-University Training Contest 7 1010 )
2016-08-09 20:18
513 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5818
分析:
对栈做三种操作:
push A x 把x元素压入A中
pop B 删除B中最上面的元素并输出该元素
merge B A合并A到B中,合并后B中的元素顺序和之前压入的顺序一样(就是从头开始把之前的压入操作都只对B执行一遍)
题解:
用一个数据结构存储值和序号,使用三个优先队列,按序号排序,合并的时候全部合并到第三个优先队列里取,同时清空A和B,合并后查询的时候,若A,B中没有元素了,就取C里的最上面元素,能够保证题中要求顺序!
-参考代码:
http://acm.hdu.edu.cn/showproblem.php?pid=5818
分析:
对栈做三种操作:
push A x 把x元素压入A中
pop B 删除B中最上面的元素并输出该元素
merge B A合并A到B中,合并后B中的元素顺序和之前压入的顺序一样(就是从头开始把之前的压入操作都只对B执行一遍)
题解:
用一个数据结构存储值和序号,使用三个优先队列,按序号排序,合并的时候全部合并到第三个优先队列里取,同时清空A和B,合并后查询的时候,若A,B中没有元素了,就取C里的最上面元素,能够保证题中要求顺序!
-参考代码:
#include<iostream> #include<cstdiO> #include<queue> using namespace std; struct stk{ int time,num; bool operator<(const stk&a) const{ return time<a.time; } }sta; priority_queue<stk> c; priority_queue<stk> a; priority_queue<stk> b; int main(){ int n; int ca = 1; while(~scanf("%d", &n) && n) { printf("Case #%d:\n", ca++); while(!a.empty()) a.pop(); while(!b.empty()) b.pop(); while(!c.empty()) c.pop(); int i=0; while(n--) { char op[10]; scanf("%s", op); if(op[1] == 'u') { scanf("%s",op); int x; scanf("%d",&x); if(op[0] == 'A') { sta.num=x; sta.time=i++; a.push(sta); } else { sta.num=x; sta.time=i++; b.push(sta); } } else if(op[1] == 'o') { scanf("%s",op); if(op[0] == 'A') { if(!a.empty()) { printf("%d\n",a.top().num); a.pop(); } else { printf("%d\n",c.top().num); c.pop(); } } else if(op[0]=='B'){ if(!b.empty()) { printf("%d\n",b.top().num); b.pop(); } else { printf("%d\n",c.top().num); c.pop(); } } } else { scanf("%s",op); while(!a.empty()) { c.push(a.top()); a.pop(); } while(!b.empty()) { c.push(b.top()); b.pop(); } scanf("%s",op); } } } }
相关文章推荐
- 2015 Multi-University Training Contest 10 (hdu 5406-5416)数据结构+dp+矩阵快速幂+bitset优化拓扑排序+(dp&树状数组)
- 2017 Multi-University Training Contest 10 1010 Schedule && HDU 6180 (贪心)
- (HDU 5818)2016 Multi-University Training Contest 7 Joint Stacks (模拟、stack)
- hdu 5818 2016 Multi-University Training Contest 7(模拟)
- hdu 5732 subway(2016 Multi-University Training Contest 1 1010)
- 贪心——Windows10 ( HDU 5802 ) ( 2016 Multi-University Training Contest 6 1010 )
- 最长递增子序列 ( LIS )——The All-purpose Zero ( HDU 5773 ) ( 2016 Multi-University Training Contest 4 1010 )
- 2016 Multi-University Training Contest 1-1001---HDU 5723 Abandoned country(DFS+最小生成树)
- 2016 Multi-University Training Contest 1 hdu 5723 Abandoned country【Kruskal+Dfs】好题
- HDU 5733 tetrahedron (2016 Multi-University Training Contest 1 计算几何)
- HDU 5723 Abandoned country(2016 Multi-University Training Contest 1的1001题)
- HDU 5729 Rigid Frameworks (from: 2016 Multi-University Training Contest)
- hdu 5348 MZL's endless loop 2015 Multi-University Training Contest 5
- hdu 3571 Hotaru's problem || 2015 Multi-University Training Contest 7 (manacher算法)
- HDU 5724 Chess(2016 Multi-University Training Contest 1的1002题)
- HDU 5336 XYZ and Drops 2015 Multi-University Training Contest 4 1010
- 2016 Multi-University Training Contest 1-1005---HDU 5727 Necklace(枚举+二分图匹配)
- 2016 Multi-University Training Contest 1 1011 hdu 5733 四面体内切球
- 2016 Multi-University Training Contest 1-1011---HDU 5733 tetrahedron(计算几何)
- 2016 Multi-University Training Contest 1 1001 hdu 5723 DFS+并查集最小生树