用两个栈模拟,并保存每个点的时间戳。每次合并的时候记录合并时的时间戳mcnt和此时的topa和topb记做ta、tb。

每次pop的时候,如果栈的top的时间戳大于mcnt,则普通地pop,否则就在两个栈ta和tb下面找时间戳最大且还没pop掉的。然后用bj[时间戳]来标记已经pop了。

#include <cstdio>
#include <cstring>
#define N 100005
using namespace std;
struct node{
int id,v;
}a[N],b[N];
int bj[N],n,topa,topb,cnt,ele,ta,tb,mcnt,cas;
char op[],st;
void popc(){
while(bj[a[ta].id])ta--;
while(bj[b[tb].id])tb--;
if(a[ta].id>b[tb].id){
printf("%d\n",a[ta].v);
bj[a[ta].id]=;
}else {
printf("%d\n",b[tb].v);
bj[b[tb].id]=;
}
}
int main() {
while(scanf("%d",&n),n){
printf("Case #%d:\n",++cas);
topa=topb=cnt=mcnt=;
memset(bj,,sizeof bj);
for(int i=;i<=n;i++){
scanf("%s %c",op,&st);
if(op[]=='u'){
scanf("%d",&ele);
if(st=='A')
a[++topa]=(node){++cnt,ele};
else
b[++topb]=(node){++cnt,ele};
}else if(op[]=='o'){
if(st=='A'){
if(a[topa].id>mcnt){
printf("%d\n",a[topa].v);
bj[a[topa].id]=;
topa--;
}
else
popc();
}else{
if(b[topb].id>mcnt){
printf("%d\n",b[topb].v);
bj[b[topb].id]=;
topb--;
}
else
popc();
}
}else if(op[]=='e'){
scanf(" %c",&st);
mcnt=cnt;
ta=topa;
tb=topb;
}
}
}
return ;
}

wa了好几发,结果原因是 “if(a[topa].id>mcnt)”写的是>=,为什么等于不可以呢,因为popc函数里没有top--,这样下一次top的值可能已经pop掉了。那如果在popc里面top--呢,也不行,因为pop栈a的ta时,可能在pop栈b,这样to pa--的话就错了。这题用优先队列也很方便。

  

#include <cstdio>
#include <queue>
#define prq priority_queue
#define pii pair<int,int>
#define mp(a,b) make_pair(a,b)
#define cl(a) while(!a.empty())a.pop();
using namespace std;
prq<pii> a,b,c;
int n,cas;
void pop(prq<pii> &q){
if(!q.empty()){
printf("%d\n",q.top().second);
q.pop();
}else{
printf("%d\n",c.top().second);
c.pop();
}
}
void push(prq<pii> &q){
while(!q.empty()){
c.push(q.top());
q.pop();
}
}
int main() {
while(scanf("%d",&n),n){
printf("Case #%d:\n",++cas);
cl(a);cl(b);cl(c);
int cnt=;
while(n--){
char op[],st;
scanf("%s %c",op,&st);
if(op[]=='u'){
int x;
scanf("%d",&x);
if(st=='A') a.push(mp(++cnt,x));
else b.push(mp(++cnt,x));
}else if(op[]=='o'){
if(st=='A') pop(a);
else pop(b);
}else{
scanf(" %c",&st);
push(a);push(b);
}
}
}
}

【HDU 5818多校】Joint Stacks的更多相关文章

  1. HDU 5818:Joint Stacks(stack + deque)

    http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description   A stack is a data ...

  2. HDU 5818 Joint Stacks(联合栈)

    HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  3. HDU 5818 Joint Stacks (优先队列)

    Joint Stacks 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5818 Description A stack is a data stru ...

  4. HDU 5818 Joint Stacks

    Joint Stacks Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. hdu 5818 Joint Stacks (优先队列)

    Joint Stacks Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  6. 2016暑假多校联合---Joint Stacks (STL)

    HDU  5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...

  7. 多校7 HDU5818 Joint Stacks

    多校7 HDU5818 Joint Stacks 题意:n次操作.模拟栈的操作,合并的以后,每个栈里的元素以入栈顺序排列 思路:开三个栈,并且用到了merge函数 O(n)的复杂度 #include ...

  8. 暑假练习赛 004 E Joint Stacks(优先队列模拟)

    Joint StacksCrawling in process... Crawling failed Time Limit:4000MS     Memory Limit:65536KB     64 ...

  9. HDU5818 Joint Stacks

    Joint Stacks                                                                       Time Limit: 8000/ ...

随机推荐

  1. Linux命令行

    linux下C编程: GCC编译常用选项: -I dir:在头文件的搜索路径列表中添加dir目录. -L dir:在库文件的搜索路径列表中添加dir目录. -fPIC:该条命令使用相对地址. shel ...

  2. Oracle 中 decode 函数用法(转)

    含义解释: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下: IF 条件=值1 THEN RETURN(翻译值1) ELSIF 条件=值2 THE ...

  3. fMRI数据分析处理原理及方法

    来源: 整理文件的时候翻到的,来源已经找不到了囧感觉写得还是不错,贴在这里保存. 近年来,血氧水平依赖性磁共振脑功能成像(Blood oxygenation level-dependent funct ...

  4. word里的代码格式,使之有底纹的效果

      实现效果: 怎么才能在word里实现这样的显示? 如何设置word里的代码格式,使之有底纹的效果

  5. TinyFrame升级之七:重构Repository和Unit Of Work

    首先,重构的想法来源于以下文章:Correct use of Repository and Unit Of Work patterns in ASP.NET MVC,因为我发现在我的框架中,对Unit ...

  6. React Native中设计主题机制

    昨天和同事讨论组件隔离性的时候讨论到关于默认样式的问题:很多情况下我们希望能够把组件设计为通用的,然后在具体项目中给他们指定一些通用的样式,譬如:背景颜色.默认字体等等.这听起来在CSS下运作起来就很 ...

  7. 准备.Net转前端开发-WPF界面框架那些事,UI快速实现法

    题外话 打开博客园,查看首页左栏的”推荐博客”,排名前五的博客分别是(此处非广告):Artech.小坦克.圣殿骑士.腾飞(Jesse).数据之巅.再看看它们博客的最新更新时间:Artech(2014- ...

  8. 代码重构之 —— 一堆if、esle 逻辑的处理

    这几天,接手一个同事的代码,关于微信接口开发的,那一堆的 if,看得哥蛋痛了,这个毛病也是很多新手容易犯的,所以特地把这次重构写出来. 下面来我们看看这个代码的问题所在,if else 里面的代码块逻 ...

  9. 一例完整的websocket实现群聊demo

    前言 业余我都会花一些时间在tcp.http和websocket等领域的学习,现在觉得有点收获,所以把一个基于websocket的群聊功能的例子提供给大家玩玩.当然这是一个很完整的例子,包括webso ...

  10. socket.io简单说明及在线抽奖demo

    socket.io简单说明及在线抽奖demo socket.io 简介 Socket.IO可以实现实时双向的基于事件的通信. 它适用于各种平台,浏览器或设备,也同样注重可靠性和速度. socket.i ...