题目:这里

题意: 两个类似于栈的列表,栈a和栈b,n个操作,push a x表示把数x放进a栈的栈底,pop b 表示将栈b的栈顶元素取出输出,并释放这个栈顶元素,merge a b表示把后面的那个

栈里的元素全部压进前面的那个栈里面,并且压入后前面的栈的所有元素按其进栈顺序排列然后后面的栈变为了空。

push和pop操作都还好,就是优先队列或者栈的操作,主要是merge操作,如果啊按照题目来模拟每次将后面的栈元素取出放入前面的栈,可能会超时,而超时的主要因素是每次都将

元素多的栈压入了元素少的栈,可以每次默认将栈1元素压入栈2,然后根据情况判断栈1和栈2到底哪个是栈a哪个是栈b,这样就解决了可能超时的问题,具体看代码。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
using namespace std; const int M = 1e5 + ;
struct node{
int x,y;
friend bool operator < (node a,node b){
return a.y<b.y;
}
}num[M];
priority_queue<node>a[]; int main()
{
int n,tem=;
while (~scanf("%d",&n)&&n){
printf("Case #%d:\n",++tem);
while (!a[].empty()) a[].pop();
while (!a[].empty()) a[].pop();
int ans=,flag=;
while (n--){
char str[],op;
scanf("%s",str);scanf(" %c",&op);
if (strcmp(str,"push")==){
scanf(" %d",&num[++ans].x);
num[ans].y=ans;
if (op=='A') a[flag].push(num[ans]);
else a[flag^].push(num[ans]);
}
else if (strcmp(str,"pop")==){
if (op=='A') printf("%d\n",a[flag].top().x),a[flag].pop();
else printf("%d\n",a[flag^].top().x),a[flag^].pop();
}
else{
char ch;
scanf(" %c",&ch);
while (!a[].empty()){
a[].push(a[].top());
a[].pop();
}
if (op=='A'&&flag==) flag=;
if (op=='B'&&flag==) flag=;
}
}
}
return ;
}

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 (优先队列)

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

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

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

  4. HDU 5818 Joint Stacks(联合栈)

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

  5. HDU 5818 Joint Stacks

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

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

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

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

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

  8. 多校7 HDU5818 Joint Stacks

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

  9. HDU5818 Joint Stacks

    Joint Stacks                                                                       Time Limit: 8000/ ...

随机推荐

  1. hpp头文件与h头文件的区别

    hpp,其实质就是将.cpp的实现代码混入.h头文件当中,定义与实现都包含在同一文件,则该类的调用者只需要include该hpp文件即可,无需再将cpp加入到project中进行编译.而实现代码将直接 ...

  2. youtube不显示其他人头像

    我用的是ss , 右键ss选择pac->编辑本地pac 把ggpht.com加入pac列表即可

  3. ssh 协议执行repo sync 报错:Permission denied (publickey)

    1.ssh key 已经添加ssh key到gerrit服务器,并且执行ssh协议的git clone可以正常克隆代码到本地,可见不是ssh key的问题. 2.manifest清单文件配置 最初在m ...

  4. 【Python与机器学习】:利用Keras进行多类分类

    多类分类问题本质上可以分解为多个二分类问题,而解决二分类问题的方法有很多.这里我们利用Keras机器学习框架中的ANN(artificial neural network)来解决多分类问题.这里我们采 ...

  5. 使用属性动画简单实现view飞入效果

    比较简单的效果,可以用来菜单飞入之类,作为记录吧, package com.test.animation; import android.app.Activity; import android.os ...

  6. eclipse安装JAVA反编译插件

    前言:在实际的开发中几乎都会使用到一些框架来辅助项目的开发工作,对于一些框架的代码我们总怀有一些好奇之心,想一探究竟,有源码当然更好了,对于有些JAR包中的代码我们就需要利用反编译工具来看一下了,下面 ...

  7. FMDBsqilte语句样式

    举例: @"create table student(stuid integer primary key autoincrement,name text,number integer,the ...

  8. Qlikview List控件

    将纵向展示变为横向展示 方法: ListBox属性分页,“外观”分页“单列”属性不要打钩,用鼠标调整控件高度,Listbox控件会自适应现实将数据打横现实.

  9. vc6 编译问题

    Compiling...main.cppLinking...MSVCRT.lib(MSVCRT.dll) : error LNK2005: _malloc already defined in LIB ...

  10. Extract QQ from iPhone and analyze it

    QQ is one of the most popular chat App in the world. Now let me show you how to extract QQ from iPho ...