题目大意:有两个栈A,B,三种操作,push进栈,pop出栈,merge A B则将B内容放进A中重新按照入栈时间形成新栈,并将B清空。
解题思路:用优先队列模拟操作。fi表示入栈的值,se为入栈序号。一开始直接模拟TLE了,需要用到滚动数组,因为merge操作有可能将一个栈很大的放到一个空栈里去,或者是一个很小的栈里去,然后不断重复这样操作就会超时了。
因为merge操作后一定会有一个栈为空的,那为何不让栈小的清空呢?故判定一下,利用滚动数组思想搞搞就可。
/* ***********************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆ ┃ 勒 ┃ ┆
┆ ┃ 戈 ┗━━━┓ ┆
┆ ┃ 壁 ┣┓┆
┆ ┃ 的草泥马 ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
struct node
{
int fi,se;
friend bool operator <(node a,node b)
{
return a.se<b.se;
}
};
char op[10];
char a,b;
int val;
int ans[500100];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int n,cas = 1;
while(~scanf("%d",&n),n)
{
priority_queue<node>que[2];
int cnt = 0,num[2];
int flag = 0;
for(int i=1;i<=n;i++)
{
scanf("%s",op);
if(op[1]=='u')
{
scanf(" %c%d",&a,&val);
node g;
g.fi = val,g.se = i;
if(a=='A')que[(0+flag)%2].push(g),num[(0+flag)%2]++;
else que[(1+flag)%2].push(g),num[(1+flag)%2]++;
}
else if(op[1]=='o')
{
scanf(" %c",&a);
node g;
if(a=='A'){g = que[(0+flag)%2].top();que[(0+flag)%2].pop();num[(0+flag)%2]--;}
else {g = que[(1+flag)%2].top();que[(1+flag)%2].pop();num[(1+flag)%2];}
ans[cnt++] = g.fi;
}
else
{
scanf(" %c %c",&a,&b);
if(a=='A')
{
node g;
if(num[(0+flag)%2] < num[(1+flag)%2]) flag = (flag+1) % 2;
while(!que[(1+flag)%2].empty())
{
g = que[(1+flag)%2].top();
que[(0+flag)%2].push(g);
que[(1+flag)%2].pop();
num[(0+flag)%2]++;
}
num[(1+flag)%2] = 0;
}
else
{
node g;
if(num[(0+flag)%2] > num[(1+flag)%2]) flag = (flag+1) % 2;
while(!que[(0+flag)%2].empty())
{
g = que[(0+flag)%2].top();
que[(1+flag)%2].push(g);
que[(0+flag)%2].pop();
num[(1+flag)%2]++;
}
num[(0+flag)%2] = 0;
}
}
}
printf("Case #%d:\n",cas++);
for(int i=0;i<cnt;i++)
printf("%d\n",ans[i]);
}
return 0;
}