HDU5818 Joint Stacks 2016 Multi-University Training Contest 7(模拟)
2016-08-09 18:21
579 查看
Joint Stacks
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 144 Accepted Submission(s): 43
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 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.
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
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
Sample Output
Case #1:
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0
Author
SYSU
Source
2016 Multi-University Training Contest 7
Recommend
wange2014
弱鸡只会写水题,这题用set乱搞一下就过了。需要注意的是合并的时候一定是把元素少的栈合并到元素多的栈。这样可以实现nlogn的复杂度,如果没有这个,最坏情况复杂度是n^2大概会爆炸。
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 144 Accepted Submission(s): 43
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 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.
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
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
Sample Output
Case #1:
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0
Author
SYSU
Source
2016 Multi-University Training Contest 7
Recommend
wange2014
弱鸡只会写水题,这题用set乱搞一下就过了。需要注意的是合并的时候一定是把元素少的栈合并到元素多的栈。这样可以实现nlogn的复杂度,如果没有这个,最坏情况复杂度是n^2大概会爆炸。
#include "cstring" #include "cstdio" #include "string.h" #include "iostream" #include "set" //#include "algorithm" using namespace std; set<pair<int,int >> stack[2]; set<pair<int,int>>::iterator it; int A,B; int main() { int n,num,x,y,cas=0; char s1[10],s2[10],op[10]; while(scanf("%d",&n)&&n) { A=0; B=1; stack[0].clear(); stack[1].clear(); printf("Case #%d:\n", ++cas); for(int i=1;i<=n;i++) { scanf("%s",op); if(strcmp(op,"push")==0) { scanf("%s %d",s1,&num); if(s1[0]=='A') stack[A].insert(make_pair(i,num)); else stack[B].insert(make_pair(i,num)); } if(strcmp(op,"pop")==0) { scanf("%s",s1); if(s1[0]=='A') x=A; else x=B; it=stack[x].end(); it--; printf("%d\n",it->second); stack[x].erase(it); } if(strcmp(op,"merge")==0) { scanf("%s%s",s1,s2); if(s1[0]=='A') { x=A; y=B; } else { x=B; y=A; } if(stack[x].size()<stack[y].size()) { swap(x,y); swap(A,B); } for(it = stack[y].begin(); it != stack[y].end(); ++it){ stack[x].insert(*it); } stack[y].clear(); } } } return 0; }
相关文章推荐
- 2016 Multi-University Training Contest 7 1010 Joint Stacks (模拟)
- 2016 Multi-University Training Contest 4 1011 Where Amazing Happens(模拟)
- (HDU 5818)2016 Multi-University Training Contest 7 Joint Stacks (模拟、stack)
- 2016 Multi-University Training Contest 8 1011 Rikka with Parenthesis II(模拟)
- 2016 Multi-University Training Contest 3 1001 Sqrt Bo (模拟)
- hdu 5818 2016 Multi-University Training Contest 7(模拟)
- hdu 5831 Rikka with Parenthesis II(2016 Multi-University Training Contest 8——模拟)
- 2016 Multi-University Training Contest 1 F.PowMod
- 2016 Multi-University Training Contest 1 G. Rigid Frameworks
- 2016 Multi-University Training Contest 4 1006 Substring(后缀数组)
- 2016 Multi-University Training Contest 1----解题报告
- 2016 Multi-University Training Contest 1 T4
- 2016 Multi-University Training Contest 2 Acperience(一直WA的小伙伴可以看看)
- 【HDU】5734 Acperience(2016 Multi-University Training Contest 2)
- hdu 5742 (2016 Multi-University Training Contest 2)
- 2016 Multi-University Training Contest 2 1012 La Vie en rose
- 2016 Multi-University Training Contest 1 1005 Necklace (hdu5727)【枚举+匹配】
- 【HDU5729 2016 Multi-University Training Contest 1G】【状压DP 容斥】Rigid Frameworks 刚性图 nm二分图的联通方案数
- 2016 Multi-University Training Contest 2 题解(待续)
- HDU 5761_2016 Multi-University Training Contest 3