HDU 5818 Joint Stacks
2016-08-10 09:51
453 查看
Joint Stacks
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 610 Accepted Submission(s): 282
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
There are multiple test cases. For each case, the first line contains an integer N(0<N≤105), indicating the number of operations. The next N lines, each contain an instruction "push", "pop" or "merge". The elements of stacks are 32-bit integers. Both A and B are empty initially, and it is guaranteed that "pop" operation would not be performed to an empty stack. N = 0 indicates the end of input.
[align=left]Output[/align]
For each case, print a line "Case #t:", where t is the case number (starting from 1). For each "pop" operation, output the element that is popped, in a single line.
[align=left]Sample Input[/align]
4
push A 1
push A 2
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2
merge A B
pop A
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2
merge B A
pop B
pop B
pop B
0
[align=left]Sample Output[/align]
Case #1:
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0
list模拟....其实和栈的作用差不多,本来想用list中的merge的方法的,但是太慢了超时.....
/* *********************************************** Author :guanjun Created Time :2016/8/9 12:30:47 File Name :p710.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 100010 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; struct node{ ll x; int id; }; bool cmp(node a,node b){ return a.id<b.id; } list<node> a,c,b,d; ll num; char s[10],ch[3],w[3]; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int n; int cas=0; while(cin>>n&&n){ printf("Case #%d:\n",++cas); a.clear(),b.clear(),c.clear(),d.clear(); int mark=0; for(int i=1;i<=n;i++){ scanf("%s %s",&s,&ch); if(strcmp(s,"push")==0){ scanf("%I64d",&num); if(ch[0]=='A')a.push_back({num,i}); else b.push_back({num,i}); } else if(strcmp(s,"pop")==0){ if(ch[0]=='A'){ if(a.empty()){ printf("%I64d\n",c.back().x); c.pop_back(); continue; } printf("%I64d\n",a.back().x); a.pop_back(); } else { if(b.empty()){ printf("%I64d\n",c.back().x); c.pop_back(); continue; } printf("%I64d\n",b.back().x); b.pop_back(); } } else{ scanf("%s",&w); while(!a.empty() && !b.empty()){ int aa=a.back().id; int bb=b.back().id; if (aa>bb){ d.push_back(a.back()); a.pop_back(); } else{ d.push_back(b.back()); b.pop_back(); } } while(!a.empty()){ d.push_back(a.back()); a.pop_back(); } while(!b.empty()){ d.push_back(b.back()); b.pop_back(); } while(!d.empty()){ c.push_back(d.back()); d.pop_back(); } } } } return 0; }
相关文章推荐
- hdu 5818 Joint Stacks 静态链表+栈
- hdu 5818 Joint Stacks 优先队列(多校)
- 2016多校7 HDU 5818 Joint Stacks
- [HDU 5818] Joint Stacks (脑洞+实现)
- HDU 5818 Joint Stacks 模拟
- HDU - 5818 - Joint Stacks
- HDU 5818 Joint Stacks(左偏树)
- hdu 5818 Joint Stacks(线段树)
- hdu 5818 Joint Stacks (优先队列)
- HDU 5818 Joint Stacks (优先队列、链表)
- hdu 5818 Joint Stacks (优先队列)
- 【set】【HDU 5818】 Joint Stacks
- HDU 5818 Joint Stacks [栈]【模拟】
- HDU 5818 Joint Stacks(栈)
- hdu 5818 Joint Stacks (优先队列)
- hdu_5818_Joint Stacks(线段树模拟)
- HDU 5818 Joint Stacks(左偏树)
- HDU 5818 Joint Stacks(其他)
- hdu 5818 Joint Stacks (模拟)
- hdu 5818 Joint Stacks 2016 Multi-University 7