HDU-5818 多校训练(巧妙模拟)
2016-08-09 21:29
239 查看
Total Submission(s): 437 Accepted Submission(s): 191
[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
[align=left]题解的方法还是很简单的,但是比赛时候不容易想到(很巧妙)[/align]
[align=left]题解:比较简单巧妙的一个做法是引入一个新的栈C,每次合并的时候就把A和B合并到C上,然后把A和B都清空. push还是按正常做,pop注意当遇到要pop的栈为空时,因为题目保证不会对空栈进行pop操作,所以这时应直接改为对C栈进行pop操作. 这样做因为保证每个元素最多只在一次合并中被处理到,pop和push操作当然也是每个元素只做一次,所以总复杂度是O(N)的. 另一种做法是用链表来直接模拟,复杂度也是O(N),但代码量稍大一些.[/align]
Joint Stacks
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 437 Accepted Submission(s): 191
[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
[align=left]题解的方法还是很简单的,但是比赛时候不容易想到(很巧妙)[/align]
[align=left]题解:比较简单巧妙的一个做法是引入一个新的栈C,每次合并的时候就把A和B合并到C上,然后把A和B都清空. push还是按正常做,pop注意当遇到要pop的栈为空时,因为题目保证不会对空栈进行pop操作,所以这时应直接改为对C栈进行pop操作. 这样做因为保证每个元素最多只在一次合并中被处理到,pop和push操作当然也是每个元素只做一次,所以总复杂度是O(N)的. 另一种做法是用链表来直接模拟,复杂度也是O(N),但代码量稍大一些.[/align]
#include<stdio.h> #include<algorithm> #include<string.h> #include<limits.h> #include<stdlib.h> #include<math.h> #include<queue> #include<map> #define inf 1 #define maxn 100005 char str1[105],str2[105],str3[105]; int n,m,k,ans;//ans记录栈顶 char flag;//记录当前合并到那个栈 struct node { int x; char y; }a[maxn]; int b[maxn],num; void pop(char stu[]) { for(int i=k-1;i>=0;i--) { if(i>ans) { if(a[i].y==stu[0]) { a[i].y=0; b[num++]=a[i].x; break; } } else { if(flag==stu[0]) { if(a[i].y!=0) { a[i].y=0; b[num++]=a[i].x; break; } } } } } void push(char stu[],int x) { a[k].x=x; a[k++].y=stu[0]; } void merge(char stu1[],char stu2[]) { ans=k-1; flag=stu1[0]; } void output() { int i; for(i=0;i<num;i++) printf("%d\n",b[i]); } int main() { int cases=0,i,x; while(scanf("%d",&n),n!=0) { k=0;ans=-1;num=0; memset(a,-1,sizeof(a)); printf("Case #%d:\n",++cases); for(i=1;i<=n;i++) { scanf(" %s",str1); if(str1[1]=='o') { scanf(" %s",str2); pop(str2); } else if(str1[1]=='u') { scanf(" %s%d",str2,&x); push(str2,x); } else if(str1[1]=='e') { scanf(" %s %s",str2,str3); merge(str2,str3); } } output(); } }
相关文章推荐
- HDU-5821 多校训练第8场-1001(巧妙模拟)
- hdu 4970 Killing Monsters(数组的巧妙运用) 2014多校训练第9场
- hdu 4915 Parenthese sequence(模拟)2014多校训练第5场
- HDU 5818 (多校 7) 模拟
- hdu 4970 Killing Monsters(数组的巧妙运用) 2014多校训练第9场
- HDU 4864 Task (2014多校联合训练第一场1004) 解题报告(贪心)
- HDU 4864Task(多校联合训练1)(贪心)
- hdu 4639 hehe 多校联合训练第四场
- HDU 4643 GSM 暑期多校联合训练第五场 1001
- hdu 4602 partition 2013多校联合训练第一场
- hdu 4901 The Romantic Hero(计数dp)2014多校训练第4场1005
- HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)
- hdu 4607 park visit 2013多校联合训练第一场
- HDU 4920(杭电多校训练#5 1010 题) Matrix multiplication(不知道该挂个什么帽子。。。)
- 多校训练hdu --Nice boat(线段树,都是泪)
- hdu 4902 Nice boat(2014多校训练第4场1006)(暴力写法)
- HDU 4862 Jump(多校联合训练1)(最小费用最大流)
- hdu 4642 fliping game 多校联合训练第四场
- hdu 4619 warm up 2 并查集或搜索都可以做出来的题 2013多校联合训练第二场
- HDU 4643 GSM 暑期多校联合训练第五场 1001