HDU 5818 模拟

来源:互联网 发布:韦德职业生涯数据 编辑:程序博客网 时间:2024/05/03 22:57

Joint Stacks

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)



Problem Description
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.
 

Input
There are multiple test cases. For each case, the first line contains an integerN(0<N105), 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.
 

Output
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.
 

Sample Input
4push A 1push A 2pop Apop A9push A 0push A 1push B 3pop Apush A 2merge A Bpop Apop Apop A9push A 0push A 1push B 3pop Apush A 2merge B Apop Bpop Bpop B 0
 

Sample Output
Case #1:21Case #2:1230Case #3:1230
题意:给你两个栈,push操作是压入,pop是弹出栈顶并输出,merge是将后面的栈合并到前面的栈,并且按压入时间排序。
题解:前两个仅仅需要模拟就行了,主要是解决merge,倒过来倒过去,很容易超时,可以定义now为A所属的栈,!now为B所属的栈,那么将元素少的栈合并到元素多的栈里,如果merge A B的时候B的元素多,那么我们把A中的倒入B中,now取反即可。用结构体保存时间。
官方题解:比较简单巧妙的一个做法是引入一个新的栈C,每次合并的时候就把A和B合并到C上,然后把A和B都清空. push还是按正常做,pop注意当遇到要pop的栈为空时,因为题目保证不会对空栈进行pop操作,所以这时应直接改为对C栈进行pop操作. 这样做因为保证每个元素最多只在一次合并中被处理到,pop和push操作当然也是每个元素只做一次,所以总复杂度是O(N)的. 另一种做法是用链表来直接模拟,复杂度也是O(N),但代码量稍大一些.
ps:官方的题解的确比鄙人的好很多,鄙人也会更加努力,尽可能的寻找更优的做法。
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<set>using namespace std;typedef long long ll;ll a[100005];set<ll>sp[2];ll cnt=0;int main(){int cas=1;while(1){ll n,i,j,x;ll aend=0;char c[6];sp[0].clear();sp[1].clear();ll now=0;set<ll>::iterator it;scanf("%lld",&n);cnt=0;if(n==0)break;printf("Case #%d:\n",cas++);while(n--){scanf("%s",c);if(c[1]=='u'){scanf("%s",c);scanf("%lld",&x);a[++aend]=x;++cnt;if(c[0]=='A')sp[now].insert(cnt);else sp[!now].insert(cnt);}else if(c[1]=='o'){scanf("%s",c);if(c[0]=='A'){it=sp[now].end();it--;printf("%lld\n",a[*it]);sp[now].erase(it);}else{it=sp[!now].end();it--;printf("%lld\n",a[*it]);sp[!now].erase(it);}}else{scanf("%s",c);if(c[0]=='A'){scanf("%s",c);if(sp[now].size()>sp[!now].size()){for(it=sp[!now].begin();it!=sp[!now].end();it++){sp[now].insert(*it);}sp[!now].clear();}else{for(it=sp[now].begin();it!=sp[now].end();it++){sp[!now].insert(*it);}sp[now].clear();now=!now;}}else{scanf("%s",c);if(sp[!now].size()>sp[now].size()){for(it=sp[now].begin();it!=sp[now].end();it++){sp[!now].insert(*it);}sp[now].clear();}else{for(it=sp[!now].begin();it!=sp[!now].end();it++){sp[now].insert(*it);}sp[!now].clear();now=!now;}}}}}return 0;}


0 0