用两个栈模拟,并保存每个点的时间戳。每次合并的时候记录合并时的时间戳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. http协议(十)实体首部字段

    1.定义 包含在请求和响应中的实体部分所使用的首部,用于补充内容的更新时间等与实体相关的信息 2.Allow 通知客户端能够支持的Request-URI指定资源的所有http方法 如果服务器接收到不支 ...

  2. Xcode里-ObjC, -all_load, -force_load

    最近在做一个项目的时候,需要使用到一个第三方库,这个库的使用向导里面特别说明,在添加完该库后,需要在Xcode的Build Settings下Other Linker Flags里面加入-ObjC标志 ...

  3. MySQL分表(Partition)学习研究报告

    最近在开发一个新的项目,可能会产生大数据量,需要对部分表进行分表操作,故来研究学习MySQL的分表功能. 由于实验报告已经写成Exlce文件了,各位看过就直接下载吧:MySQL分表分析报告.xls 以 ...

  4. Linux操作系统下三种配置环境变量的方法

    现在使用linux的朋友越来越多了,在linux下做开发首先就是需要配置环境变量,下面以配置java环境变量为例介绍三种配置环境变量的方法. 1.修改/etc/profile文件 如果你的计算机仅仅作 ...

  5. python 二叉树

    class Node(object): def __init__(self, data=None, left=None, right=None): self.data = data self.left ...

  6. swift 初探NSURLSession

    进行封装, 新建一个类.network class Network1: NSObject { // 没有参数+结果的get  自定义 HTTP method 和 URL+闭包 static func ...

  7. 开发WP版本的大菠萝英雄榜

    前言 想当年Team有无数人在玩大菠萝,我被忽悠进来做肉盾,选了蛮子,从1.0开始,经历了103.105.108.2.0.2.1.这个游戏对我最大的帮助是学习了不同的技术,比如XAML.比如xcode ...

  8. [BZOJ2730][HNOI2012]矿场搭建(求割点)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2730 分析: 如果坍塌的点不是割点,那没什么影响,主要考虑坍塌的点是割点的情况. 显然 ...

  9. SqlServer中——查找杀死阻塞进程

    查找阻塞进程: SELECT blocking_session_id '阻塞进程的ID', wait_duration_ms '等待时间(毫秒)', session_id '(会话ID)' FROM ...

  10. Set Php show errors

    php中的Error等级分成16类,用一个16位的数值表示这16种集合元素.下面是从php.ini中截取的: ; Error Level Constants: ; E_ALL - All errors ...