hdu 5818 Joint Stacks (模拟)
来源:互联网 发布:日语背单词软件 编辑:程序博客网 时间:2024/05/09 02:10
Joint Stacks
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 154 Accepted Submission(s): 50
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.
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
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
合并的时候保留 A B之中最大位置的尾指针 并且把该位置标记下
之后的 PUSH 照常
POP的时候注意下是否有标记 没有就照常
有的话 把尾指针的位置减一 标记位置往前推
还有 ARR中POP掉的位置 记为 -1 在有标记的POP的时候注意下
#include<iostream>#include<algorithm>#include<cstdlib>#include<cctype>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<set>#include<map>#include<queue>#include<cmath>#include<bitset>#define pi acos(-1.0)#define inf 1<<29#define INF 0x3fffffff#define zero 1e-8const int li[] = { -1, 0, 1, 0};const int lj[] = {0, -1, 0, 1};using namespace std;const int N = 100000 + 107;int arr[N], from[N];bool flag[N];int pointa, pointb, cnt;string str1 = "push", str2 = "pop", str3 = "merge";void init(){ memset(flag,0,sizeof(flag)); memset(from, -1, sizeof(from)); cnt = 0; pointa = pointb = 0;}int n;int main(){ int c=1; while (~scanf("%d", &n) && n) { init(); printf("Case #%d:\n",c++); for (int i = 0; i < n; ++i) { char l[10], r[10]; scanf("%s%s", l, r); if (l == str1) { int num; scanf("%d", &num); arr[++cnt] = num; if (r[0] == 'A') { from[cnt] = pointa; pointa = cnt; } else { from[cnt] = pointb; pointb = cnt; } } if (l == str2) { if (r[0]=='A'){ int tip=0; while (1){ if (!flag[pointa]) break; if (flag[pointa]&&arr[pointa]!=-1) { tip=1; break; } pointa--; flag[pointa]=1; } printf("%d\n", arr[pointa]); arr[pointa]=-1; if (!tip) pointa = from[pointa]; else { pointa--; flag[pointa]=1; } }else { int tip=0; while (1){ if (!flag[pointb]) break; if (flag[pointb]&&arr[pointb]!=-1) { tip=1; break; } pointb--; flag[pointb]=1; } printf("%d\n", arr[pointb]); arr[pointb]=-1; if (!tip) pointb = from[pointb]; else { pointb--; flag[pointb]=1; } } } if (l == str3) { char m[10]; scanf("%s", m); if (r[0] == m[0]) continue; int maxx=pointa>pointb?pointa:pointb; flag[maxx]=true; if (r[0] == 'A') { pointa=maxx; pointb=0; } else { pointb=maxx; pointa=0; } } } } return 0;}
0 0
- 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 栈模拟
- 模拟(Joint Stacks,HDU 5818)
- hdu 5818 Joint Stacks
- HDU 5818 Joint Stacks(模拟 || 优先队列 || 左式堆)
- hdu 5818 Joint Stacks 栈
- HDU 5818 Joint Stacks(左偏树)
- 【HDU5818】Joint Stacks(模拟)
- 2016多校7 HDU 5818 Joint Stacks
- hdu 5818 Joint Stacks(线段树)
- [HDU 5818] Joint Stacks (脑洞+实现)
- hdu 5818 Joint Stacks (优先队列)
- hdu 5818 Joint Stacks (优先队列)
- 坚持#第10天~干掉BUG
- 证件、表格英语
- 使用symbolicatecrash分析crash文件
- 当androidstudio 工具栏出问题比如移动,少东西之类的
- JSP中的jstl
- hdu 5818 Joint Stacks (模拟)
- Oozie和Azkaban的技术选型和对比
- HDU 5810 Balls ans Boxes(二项分布)
- ElasticSearch第2课之核心概念介绍
- 深入Java集合学习系列:HashMap的实现原理
- C#中,论double与decimal
- I/O缓冲区
- 重新组织数据(三)
- zzulioj 1902: 985的因子对难题 [思维]