1 多校7 HDU5818 Joint Stacks
2 题意:n次操作。模拟栈的操作,合并的以后,每个栈里的元素以入栈顺序排列
3 思路:开三个栈,并且用到了merge函数
4 O(n)的复杂度
5
6 #include <bits/stdc++.h>
7 using namespace std;
8 #define LL long long
9 const int inf = 0x3f3f3f3f;
10 const int MOD =998244353;
11 const int N =100010;
12 #define clc(a,b) memset(a,b,sizeof(a))
13 const double eps = 1e-7;
14 void fre() {freopen("in.txt","r",stdin);}
15 void freout() {freopen("out.txt","w",stdout);}
16 inline int read() {int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;}
17 int n;
18 int sta[3][N],top[3];
19 char op[10],s[5],s1[5];
20 int x[N];
21 void fun(){
22 top[0]=top[1]=top[2]=0;
23 for(int i=0;i<n;i++){
24 scanf("%s%s",op,s);
25 int a=s[0]-'A';
26 if(op[1]=='u'){
27 scanf("%d",&x[i]);
28 sta[a][top[a]++]=i;
29 }
30 else if(op[1]=='o'){
31 if(!top[a]) a=2;
32 printf("%d\n",x[sta[a][--top[a]]]);
33 }
34 else {
35 scanf("%s",s1);
36 top[2]=merge(sta[0],sta[0]+top[0],sta[1],sta[1]+top[1],sta[2]+top[2])-sta[2];
37 top[0]=top[1]=0;
38 }
39 }
40 }
41 int main(){
42 int cas=1;
43 while(~scanf("%d",&n),n){
44 printf("Case #%d:\n", cas++);
45 fun();
46 }
47 return 0;
48 }