hdu5818(2016多校第7场,模拟)

202 篇文章 0 订阅
154 篇文章 0 订阅

Joint Stacks

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


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<N105) , 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

思路:引入一个新的栈C,每次合并的时候就把A和B合并到C上,然后把A和B都清空. push还是按正常做,pop注意当遇到要pop的栈为空时,因为题目保证不会对空栈进行pop操作,所以这时应直接改为对C栈进行pop操作. 这样做因为保证每个元素最多只在一次合并中被处理到,pop和push操作当然也是每个元素只做一次,所以总复杂度是O(N)的.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct point
{
    int num,id;
} S[100000+50],A[100000+50],B[100000+50];
char str[10],ntr[10];
int Atop,Btop,Stop,Atip,Btip;
int main()
{
    int T,cas=1,sum,sumA,sumB,sumS;
    while(~scanf("%d",&T)&&T)
    {
        memset(A,0,sizeof(A));
        memset(B,0,sizeof(B));
        memset(S,0,sizeof(S));
        sum=0,sumA=0,sumB=0,sumS=0;
        Atop=0,Atip=0;
        Btop=0,Btip=0;
        Stop=0;
        printf("Case #%d:\n",cas++);
        for(int i=0; i<T; i++)
        {
            scanf("%s",str);
            if(str[1]=='u')
            {
                int n;
                scanf("%s",ntr);
                scanf("%d",&n);
                if(ntr[0]=='A')
                {
                    A[Atop].num=n;
                    A[Atop].id=i;
                    sum++;
                    sumA++;
                    Atop++;
                }
                else
                {
                    B[Btop].num=n;
                    B[Btop].id=i;
                    sum++;
                    sumB++;
                    Btop++;
                }
            }
            else if(str[1]=='o')
            {
                scanf("%s",ntr);
                if(ntr[0]=='A')
                {
                    if(sum>sumB+sumS)
                    {
                        printf("%d\n",A[Atop-1].num);
                        A[Atop-1].num=0;
                        A[Atop-1].id=0;
                        Atop--;
                        sumA--;
                        sum--;
                    }
                    else
                    {
                        printf("%d\n",S[Stop-1].num);
                        S[Stop-1].num=0;
                        S[Stop-1].id=0;
                        Stop--;
                        sumS--;
                        sum--;
                    }
                }
                else
                {
                    if(sum>sumA+sumS)
                    {
                        printf("%d\n",B[Btop-1].num);
                        B[Btop-1].num=0;
                        B[Btop-1].id=0;
                        Btop--;
                        sumB--;
                        sum--;
                    }
                    else
                    {
                        printf("%d\n",S[Stop-1].num);
                        S[Stop-1].num=0;
                        S[Stop-1].id=0;
                        Stop--;
                        sumS--;
                        sum--;
                    }
                }
            }
            else
            {
                scanf("%s%*s",ntr);
                sumS+=sumA+sumB;
                sumA=0,sumB=0;
                while(Atip<Atop&&Btip<Btop)
                {
                    //cout<<Atip<<" "<<Btip<<endl;
                    if(A[Atip].id<B[Btip].id)
                    {
                        S[Stop].num=A[Atip].num;
                        S[Stop].id=A[Atip].id;
                        Stop++;
                        Atip++;
                    }
                    else
                    {
                        S[Stop].num=B[Btip].num;
                        S[Stop].id=B[Btip].id;
                        Stop++;
                        Btip++;
                    }
                }
                while(Atip<Atop)
                {
                    //cout<<"***";
                    S[Stop].num=A[Atip].num;
                    S[Stop].id=A[Atip].id;
                    Stop++;
                    Atip++;
                }
                while(Btip<Btop)
                {
                    //cout<<"///";
                    S[Stop].num=B[Btip].num;
                    S[Stop].id=B[Btip].id;
                    Stop++;
                    Btip++;
                }
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值