多校赛第7场 hdu5818 Joint Stacks 双向链表
来源:互联网 发布:国外windows优化软件 编辑:程序博客网 时间:2024/04/28 07:05
题目
A stack is a data structure in which all insertions and deletions of entries are made at one end, called the “top” of the stack. The last entry which is inserted is the first one that will be removed. In another word, the operations perform in a Last-In-First-Out (LIFO) manner.
A mergeable stack is a stack with “merge” operation. There are three kinds of operation as follows:
- push A x: insert x into stack A
- pop A: remove the top element of stack A
- merge A B: merge stack A and B
After an operation “merge A B”, stack A will obtain all elements that A and B contained before, and B will become empty. The elements in the new stack are rearranged according to the time when they were pushed, just like repeating their “push” operations in one stack. See the sample input/output for further explanation.
Given two mergeable stacks A and B, implement operations mentioned above.
这个题有很多方法,不过双向链表复杂度比较低
题意
就是模拟栈的操作,但是有一个合并的操作。
思路
我用四个链表进行维护,A和B存的是指向大链表的指针,也就是说最初A和B就是已经合并的状态,这样免去的合并的时间。
代码
#include<bits/stdc++.h>//orz zhen shi qiangusing namespace std;typedef long long ll;int N;char s[10];char s2[5];char s3[5];typedef list<int>::iterator lit; list<int> his, now;list<lit> A, B;list<lit> *a, *b;int main() { int cs = 1; while(~scanf("%d",&N) && N) { printf("Case #%d:\n",cs++); A.clear(); B.clear(); his.clear(); now.clear(); a = &A, b = &B; while(N--) { scanf("%s%s",s,s2); if(s[1] == 'u') { int n; scanf("%d",&n); if(s2[0] == 'A') { now.push_back(n); lit t = now.end(); t--; a->push_back(t); } else { now.push_back(n); lit t = now.end(); t--; b->push_back(t); } } else if(s[1] == 'e') { scanf("%s",s3); his.insert(his.end(), now.begin(), now.end()); now.clear(); //if(s2[0] == 'B') swap(a, b); a->clear(); b->clear(); //printf("??%d\n",a->empty()); } else { //printf("s2[0]=%c\n",s2[0]); if(s2[0] == 'A') { //printf("-------%d\n",a->empty()); if(A.empty()) { printf("%d\n",his.back()); his.pop_back(); } else { printf("%d\n",*A.back()); now.erase(A.back()); A.pop_back(); } } else { if(B.empty()) { printf("%d\n",his.back()); his.pop_back(); } else { printf("%d\n",*B.back()); now.erase(B.back()); B.pop_back(); } } } } } return 0;}
- 多校赛第7场 hdu5818 Joint Stacks 双向链表
- 【HDU5818】Joint Stacks(模拟)
- hdu5818 Joint Stacks 数据结构
- HDU5818 Joint Stacks
- HDU5818-Joint Stacks
- HDU5818 Joint Stacks 2016 Multi-University Training Contest 7(模拟)
- HDU5818(多校第七场)——Joint Stacks(优先队列,左偏树)
- hdu5818 Joint Stacks(优先队列)
- 2016多校训练Contest7: 1010 Joint Stacks hdu5818
- hdu 5818 Joint Stacks 静态链表+栈
- Joint Stacks
- hdu5818(2016多校第7场,模拟)
- 2016多校7 HDU 5818 Joint Stacks
- hdu 5819 Joint Stacks
- Joint Stacks--模仿栈
- hdu 5818 Joint Stacks
- HDU 5818 多校第七场 1010 Joint Stacks (线段树)
- hdu 5818 Joint Stacks 2016 Multi-University 7
- 【其它】Coder专属的15个谜语 你能猜中几个?
- 暑期dp46道(17)--HDOJ1114 Piggy Bank 完全背包
- 【iOS开发】用户点击频繁,多个异步网络请求取消问题?
- HDU 1002 A + B Problem II
- 华为OJ——字符个数统计
- 多校赛第7场 hdu5818 Joint Stacks 双向链表
- 【iOS开发】UITableView 默认选择某行的方法
- Android之微信第三方登录
- C\C++中istringstream、ostringstream、stringstream 类介绍 .
- Python的编码注释# -*- coding:utf-8 -*-
- EasyPR--一个开源的中文车牌识别系统
- 【其它】欧洲汽车品牌
- UVa220 ACM/ICPC 1992黑白棋
- hdoj5328【尺取】