您的位置:首页 > 其它

2016多校7 HDU 5818 Joint Stacks

2016-08-09 18:45 344 查看

2016多校联合训练#7

HDU 5818 Joint Stacks

优先队列

传送门:HDU

题意

给出两个栈A,B(初始时为空),有三种操作:push、pop、merge。

其中merge是按照A B中元素进栈的相对顺序来重排的。

思路

三个优先队列,按照进入的时间顺序作为优先级。

合并时清空A,B,全加入到C中。

每次pop时,如果被pop的队列有元素,那么pop;没有就pop队列C。

代码

#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
#include <stack>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;

const int MAXN=200007;
const double eps=1e-8;
const int oo=2000000007;
struct ppp{
int first,second;
ppp (){};
ppp (int _first,int _second)
{
first=_first;
second=_second;
}
bool operator <(const ppp &a) const
{
return second<a.second;
}
};
typedef struct ppp P;
int main()
{
int test=1;
int n;
while(~scanf("%d",&n)&&n!=0)
{
printf("Case #%d:\n",test++);
priority_queue<P> a,b,c;
while(!a.empty()) a.pop();
while(!b.empty()) b.pop();
while(!c.empty()) c.pop();
char p[10],q[10];
int cnt=1;
for(int i=0;i<n;i++)
{
scanf("%s%s",p,q);
if(p[1]=='u')
{
int t;
scanf("%d",&t);
if(q[0]=='A') a.push(P(t,cnt));
else b.push(P(t,cnt));
cnt++;
}
else if(p[1]=='o')
{
if(q[0]=='A')
{
if(a.empty())
{
printf("%d\n",c.top().first);
c.pop();
}
else
{
printf("%d\n",a.top().first);
a.pop();
}
}
else if(q[0]=='B')
{
if(b.empty())
{
printf("%d\n",c.top().first);
c.pop();
}
else
{
printf("%d\n",b.top().first);
b.pop();
}
}
}
else
{
char g[10];
scanf("%s",g);
while(!a.empty()&&!b.empty())
{
if(a.top().second>b.top().second)
{
c.push(b.top());
b.pop();
}
else
{
c.push(a.top());
a.pop();
}
}
while(!a.empty())
{
c.push(a.top());
a.pop();
}
while(!b.empty())
{
c.push(b.top());
b.pop();
}
}
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: