HDU 5818 Joint Stacks(其他)
2017-07-13 19:47
423 查看
Joint Stacks
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1806 Accepted Submission(s): 814
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
题意:
入栈,出栈,合并栈。
point:
使A栈为0,B栈为1,没合并之前很简单,合并之后使被合并的新元素标记为2,依此类推,出新栈就找2,出旧栈就找<=1的。就不需要找旧元素去更改归属了。
#include <stdio.h> #include <string.h> #include<iostream> using namespace std; #define ll long long struct node { int now; int f; }a[100006]; int ff[100006]; int num; void f1(int now) { for(int i=num;i>=1;i--) { if(a[i].f==now&&!ff[i]) { printf("%d\n",a[i].now); ff[i]=1; return; } } } void f2(int now) { for(int i=num;i>=1;i--) { if(a[i].f<now&&!ff[i]) { printf("%d\n",a[i].now); ff[i]=1; return; } } } int flag[200]; int main() { int n; int p=0; while(~scanf("%d",&n)&&n) { printf("Case #%d:\n",++p); memset(ff,0,sizeof ff); flag['A']=0; flag['B']=1; int now=1; num=0;char c,hou='o'; for(int i=1;i<=n;i++) { char s[10]; cin>>s; cin>>c; if(s[1]=='u') { int b; scanf("%d",&b); a[++num].now=b; a[num].f=flag[c]; } else if(s[1]=='e') { cin>>hou; flag[hou]=++now; } else if(s[1]=='o') { if(hou=='o') { if(c=='A') { f1(0); } else { f1(1); } } else if(c==hou) { f1(now); } else { f2(now); } } } } }
相关文章推荐
- [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(左偏树)
- hdu 5818 Joint Stacks (优先队列)
- hdu 5818 Joint Stacks (模拟)
- hdu 5818 Joint Stacks 2016 Multi-University 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