Joint Stacks

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1819    Accepted Submission(s): 819

Problem Description
A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another word, the operations perform in a Last-In-First-Out (LIFO) manner.
A mergeable stack is a stack with "merge" operation. There are three kinds of operation as follows:

- push A x: insert x into stack A
- pop A: remove the top element of stack A
- merge A B: merge stack A and B

After an operation "merge A B", stack A will obtain all elements that A and B contained before, and B will become empty. The elements in the new stack are rearranged according to the time when they were pushed, just like repeating their "push" operations in one stack. See the sample input/output for further explanation.
Given two mergeable stacks A and B, implement operations mentioned above.

 
Input
There are multiple test cases. For each case, the first line contains an integer N(0<N≤105), indicating the number of operations. The next N lines, each contain an instruction "push", "pop" or "merge". The elements of stacks are 32-bit integers. Both A and B are empty initially, and it is guaranteed that "pop" operation would not be performed to an empty stack. N = 0 indicates the end of input.
Output
For each case, print a line "Case #t:", where t is the case number (starting from 1). For each "pop" operation, output the element that is popped, in a single line.
 
Sample Input
 4 
push A 1 
push A 2 
pop A 
pop A 

push A 0 
push A 1 
push B 3 
pop A 
push A 2 
merge A B 
pop A 
pop A 
pop A 

push A 0 
push A 1 
push B 3 
pop A 
push A 2 
merge B A 
pop B 
pop B 
pop B 
0
 
Sample Output
Case #1: 


Case #2: 




Case #3: 



0
 
Source
 
题解一:
因为merge操作的耗时多,如果纯模拟会tle,所以我们利用滚动数组,在merge的时候,把少的合并到多的地方。
#include <iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
char ch[],str1[],str2[];
int n,x,id,id1,id2; struct node
{
int k,ti;
node(int x,int y){k=x; ti=y;}
}; struct cmp
{
bool operator()(node a,node b)
{
return a.ti<b.ti;
}
}; int main()
{
int cas=;
while(~scanf("%d",&n))
{
if (n==) break;
printf("Case #%d:\n",++cas);
priority_queue<node,vector<node>,cmp> a[];
int flag=;
for(int i=;i<=n;i++)
{
scanf("%s",&ch);
if (ch[]=='u')
{
scanf("%s%d",&str1,&x);
id=str1[]-'A'+;
id=(id+flag)%;
a[id].push(node(x,i));
} else
if (ch[]=='o')
{
scanf("%s",&str1);
id=str1[]-'A'+;
id=(id+flag)%;
printf("%d\n",a[id].top());
a[id].pop();
} else
{
scanf("%s%s",&str1,&str2);
id1=str1[]-'A'+; id1=(id1+flag)%;
id2=str2[]-'A'+; id2=(id2+flag)%;
if (a[id1].size()>=a[id2].size())
{
while(!a[id2].empty())
{
a[id1].push(a[id2].top());
a[id2].pop();
}
} else
{
while(!a[id1].empty())
{
a[id2].push(a[id1].top());
a[id1].pop();
}
flag=(flag+)%;
}
}
}
}
return ;
}

题解二:

充分利用题目中的说明:“不会pop空了的栈”,这样我们可以开三个优先队列。

#include <iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std; struct node
{
int k,ti;
node(int x,int y){k=x; ti=y;}
};
struct cmp
{
bool operator()(node a,node b)
{
return a.ti<b.ti;
}
};
char ch[],str1[],str2[];
int n,x; int main()
{
int cas=;
while(~scanf("%d",&n))
{
if (n==) break;
printf("Case #%d:\n",++cas);
priority_queue<node,vector<node>,cmp> A,B,C;
for(int i=;i<=n;i++)
{
scanf("%s",&ch);
if (ch[]=='u')
{
scanf("%s%d",&str1,&x);
if (str1[]=='A') A.push(node(x,i));
else B.push(node(x,i));
} else
if (ch[]=='o')
{
scanf("%s",&str1);
if(str1[]=='A')
{
if(!A.empty()) {printf("%d\n",A.top().k); A.pop();}
else {printf("%d\n",C.top().k); C.pop();}
} else
{
if(!B.empty()) {printf("%d\n",B.top().k); B.pop();}
else {printf("%d\n",C.top().k); C.pop();}
}
} else
{
scanf("%s%s",&str1,&str2);
while(!A.empty())
{
C.push(A.top());
A.pop();
}
while(!B.empty())
{
C.push(B.top());
B.pop();
}
}
}
}
return ;
}

hdu 5818 Joint Stacks (优先队列)的更多相关文章

  1. HDU 5818 Joint Stacks(联合栈)

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

  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 ——(栈的操作模拟,优先队列)

    题意:有两个栈A和B,有3种操作:push,pop,merge.前两种都是栈的操作,最后一种表示的是如果“merge A B”,那么把B中的元素全部放到A中,且满足先入后出的栈原则. 分析:显然,我们 ...

  5. HDU 5818 Joint Stacks(左偏树)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5818 [题目大意] 给出两个栈A B(初始时为空),有三种操作: push.pop.merge. ...

  6. HDU - 5818 Joint Stacks 比较大の模拟,stack,erase

    https://vjudge.net/problem/HDU-5818 题意:给你两个栈AB,有常规push,pop操作,以及一个merge操作,merge A B 即将A.B的元素按照入栈顺序全部出 ...

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

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

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

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

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

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

随机推荐

  1. 【web Api性能提升技巧】(2)从DataReader手工创建Json字符串

    这个思路是从 一篇文章,关于<提升web api的性能>上看到的.自己实践了一番,写下步骤. 传统的DataReader是遵循这样的一个步骤: While(reader.Read()) { ...

  2. TOSCA自动化测试工具--怎么写自动化用例

    1.查看一下要测试的对象属性 2.

  3. Java并发编程:并发容器之ConcurrentHashMap(转)

    本文转自:http://www.cnblogs.com/dolphin0520/p/3932905.html Java并发编程:并发容器之ConcurrentHashMap(转载) 下面这部分内容转载 ...

  4. Oracle中验证非空的函数NVL(),NVL2()总结

    1.NVL()函数 NVL函数的格式如下: NVL(expr1,expr2) 含义是:如果oracle第一个参数为空那么显示第二个参数的值,如果第一个参数的值不为空,则显示第一个参数本来的值. 2 N ...

  5. Redis学习笔记之Redis单机,伪集群,Sentinel主从复制的安装和配置

    0x00 Redis简介 Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure server). Redis的键值 ...

  6. git失败案例

    哈哈哈,git终于能push了,哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈 我怀疑是系统版本的问题,之前一直不没能pu ...

  7. 20145201李子璇《网络对抗》逆向及Bof基础实践

    20145201李子璇<网络对抗>逆向及Bof基础实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回 ...

  8. 2009-2010 ACM-ICPC, NEERC, Western Subregional Contest

    2009-2010 ACM-ICPC, NEERC, Western Subregional Contest 排名 A B C D E F G H I J K L X 1 0 1 1 1 0 1 X ...

  9. 0ctf2017-pages-choices

    Pages 题目来自于CCS 2016 <Prefetch Side-Channel Attacks: Bypassing SMAP and Kernel ASLR>,利用intel pr ...

  10. nsis源码 nsisdialogdesigner

    ; 安装程序初始定义常量!define PRODUCT_NAME "nsisdialogdesigner"!define PRODUCT_VERSION "1.1.3&q ...