【HDU 5818多校】Joint Stacks
用两个栈模拟,并保存每个点的时间戳。每次合并的时候记录合并时的时间戳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的更多相关文章
- HDU 5818:Joint Stacks(stack + deque)
http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description A stack is a data ...
- HDU 5818 Joint Stacks(联合栈)
HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 5818 Joint Stacks (优先队列)
Joint Stacks 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5818 Description A stack is a data stru ...
- HDU 5818 Joint Stacks
Joint Stacks Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- hdu 5818 Joint Stacks (优先队列)
Joint Stacks Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 2016暑假多校联合---Joint Stacks (STL)
HDU 5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...
- 多校7 HDU5818 Joint Stacks
多校7 HDU5818 Joint Stacks 题意:n次操作.模拟栈的操作,合并的以后,每个栈里的元素以入栈顺序排列 思路:开三个栈,并且用到了merge函数 O(n)的复杂度 #include ...
- 暑假练习赛 004 E Joint Stacks(优先队列模拟)
Joint StacksCrawling in process... Crawling failed Time Limit:4000MS Memory Limit:65536KB 64 ...
- HDU5818 Joint Stacks
Joint Stacks Time Limit: 8000/ ...
随机推荐
- [No000063]Python格式化输出
python print格式化输出. 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果: 2.打印整数 print ...
- Eclipse 搜索插件 instasearch
http://marketplace.eclipse.org/content/instasearch
- 原创:Eclipse 上网代理设置(亲测有效)
直接上图:
- Json 基于jQuery+JSON的省市联动效果
helloweba.com 作者:月光光 时间:2012-09-12 21:57 标签: jQuery JSON Ajax 省市联动 省市区联动下拉效果在WEB中应用非常广泛,尤其在一些 ...
- fancybox的配置项
Fancybox的API和配置选项说明 属性名 默认值 简要说明 padding 10 浏览框内边距,和css中的padding一个意思 margin 20 浏览框外边距,和css中的margin一个 ...
- JavaScript Number 对象
JavaScript Number 对象 Number 对象 Number 对象是原始数值的包装对象. Number 创建方式 new Number(). 语法 var num = new Numbe ...
- sg函数与博弈论2
参考链接: http://blog.sina.com.cn/s/blog_51cea4040100h3l9.html 这篇主要就是讲anti-sg.multi-sg和every-sg的. 例1 poj ...
- Apache mod_rewrite规则重写的标志一览
1) R[=code](force redirect) 强制外部重定向 强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定,将用缺省 ...
- Java核心技术点之动态代理
本篇博文会从代理的概念出发,介绍Java中动态代理技术的使用,并进一步探索它的实现原理.由于个人水平有限,叙述中难免出现不清晰或是不准确的地方,希望大家可以指正,谢谢大家:) 一.概述 1. 什么是代 ...
- Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例
概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...