hdu-5818 Joint Stacks(模拟)

题目链接:

Joint Stacks

Time Limit: 8000/4000 MS (Java/Others)  

  Memory Limit: 65536/65536 K (Java/Others)

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
9
push A 0
push A 1
push B 3
pop A
push A 2
merge A B
pop A
pop A
pop A
9
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:

2

1

Case #2:

1

2

3

0

Case #3:

1

2

3

0

题意:

现在有两个栈,有入栈和出栈和合并的操作,问每次出栈的数字是多少;

思路:

开三个栈,模拟这几种操作,当出栈时发现当前栈为空时就跳到第三个栈,

想用链表模拟可是感觉不好写;

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar());
    for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + ‘0‘);
    putchar(‘\n‘);
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e18;
const int N=1e5+10;
const int maxn=5e3+4;
const double eps=1e-12;

stack<int>a,b,c,d;

char s[10],st[10],str[10];

LL temp[N];
int main()
{
    int Case=0;
    LL x;
    while(1)
    {
        int n,cnt=0;
        read(n);
        if(n==0)break;
        while(!a.empty())a.pop();
        while(!b.empty())b.pop();
        while(!c.empty())c.pop();
        printf("Case #%d:\n",++Case);
        For(i,1,n)
        {
            scanf("%s%s",s,str);
            if(s[0]==‘p‘)
            {
                if(s[1]==‘u‘)
                {
                    scanf("%lld",&x);
                    temp[++cnt]=x;
                    if(str[0]==‘A‘)a.push(cnt);
                    else b.push(cnt);
                }
                else
                {
                    if(str[0]==‘A‘&&!a.empty())
                    {
                        printf("%lld\n",temp[a.top()]);
                        a.pop();
                    }
                    else if(str[0]==‘B‘&&!b.empty())
                    {
                        printf("%lld\n",temp[b.top()]);
                        b.pop();
                    }
                    else
                    {
                        printf("%lld\n",temp[c.top()]);
                        c.pop();
                    }
                }
            }
            else
            {
                scanf("%s",st);
                while(!a.empty()||!b.empty())
                {

                    int A,B;
                    if(a.empty())A=0;
                    else A=a.top();
                    if(b.empty())B=0;
                    else B=b.top();
                    if(A>B)
                    {
                        a.pop();
                        d.push(A);
                    }
                    else
                    {
                        b.pop();
                        d.push(B);
                    }
                }
                while(!d.empty())
                {
                    c.push(d.top());
                    d.pop();
                }
            }
        }
    }
    return 0;
}

  

时间: 2024-10-03 21:57:02

hdu-5818 Joint Stacks(模拟)的相关文章

HDU 5818 Joint Stacks(联合栈)

HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description 题目描述 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of

HDU 5818 Joint Stacks ——(栈的操作模拟,优先队列)

题意:有两个栈A和B,有3种操作:push,pop,merge.前两种都是栈的操作,最后一种表示的是如果“merge A B”,那么把B中的元素全部放到A中,且满足先入后出的栈原则. 分析:显然,我们给每一个节点配备一个时间戳即可.我一开始的思路是直接开两个优先队列进行直接模拟,merge操作就是把一个栈的元素全部倾倒到另一个栈内,但是会出现的问题是,如果有一个状态A和B的元素都相当多了,并且反复的进行merge操作,那么每一次merge都意味着大量的元素进出,肯定会超时的.因此,我们需要优化,

HDU - 5818 Joint Stacks 想法题 关键

http://codeforces.com/problemset/problem/669/D 题意:n个数1~N围成一个圈.q个操作包括操作1:输入x, 所有数右移x.操作2:1,2位置上的数(swap(a[1], a[2])交换:3,4交换.... 题解:观察,发现所有奇数行为都是一样的,偶数同理,(对于操作1 两者相同,对于操作2 奇数位++,偶数位上--: 模拟1,2,即可,然后依次输出其它数字即可.(看清 ac代码: #define _CRT_SECURE_NO_WARNINGS #in

HDU 5818 Joint Stacks

搞了第三个栈来表示合并之后的.偷懒写了一个优先队列. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #inc

HDU 5818 Joint Stacks(左偏树)

题目链接:点击打开链接 思路: 该题的关键是怎么把两个栈合并, 我们可以使用一种叫左偏树的数据结构, 满足堆的性质和集合的性质,支持在O(logn)的复杂度下进行删除堆顶元素, 插入一个元素,合并两个堆. 细节参见代码: #include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; const int maxn = 152400; P v[maxn]; int tot, u, a, b, l[maxn

HDU 5818:Joint Stacks(stack + deque)

http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks 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

暑假练习赛 004 E Joint Stacks

Joint StacksCrawling in process... Crawling failed Time Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 5818 uDebug Description Input Output Sample Input Sample Output Hint Description A stack is a d

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

HDU  5818 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 wor

多校7 HDU5818 Joint Stacks

1 多校7 HDU5818 Joint Stacks 2 题意:n次操作.模拟栈的操作,合并的以后,每个栈里的元素以入栈顺序排列 3 思路:开三个栈,并且用到了merge函数 4 O(n)的复杂度 5 6 #include <bits/stdc++.h> 7 using namespace std; 8 #define LL long long 9 const int inf = 0x3f3f3f3f; 10 const int MOD =998244353; 11 const int N =