• hdu 5818 Joint Stacks (优先队列)


    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<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 

    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[10],str1[10],str2[10];
    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=0;
        while(~scanf("%d",&n))
        {
            if (n==0) break;
            printf("Case #%d:
    ",++cas);
            priority_queue<node,vector<node>,cmp> a[3];
            int flag=1;
            for(int i=1;i<=n;i++)
            {
                scanf("%s",&ch);
                if (ch[1]=='u')
                {
                    scanf("%s%d",&str1,&x);
                    id=str1[0]-'A'+1;
                    id=(id+flag)%2;
                    a[id].push(node(x,i));
                } else
                if (ch[1]=='o')
                {
                    scanf("%s",&str1);
                    id=str1[0]-'A'+1;
                    id=(id+flag)%2;
                    printf("%d
    ",a[id].top());
                    a[id].pop();
                } else
                {
                    scanf("%s%s",&str1,&str2);
                    id1=str1[0]-'A'+1; id1=(id1+flag)%2;
                    id2=str2[0]-'A'+1; id2=(id2+flag)%2;
                    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+1)%2;
                    }
                }
            }
        }
        return 0;
    }

    题解二:

    充分利用题目中的说明:“不会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[10],str1[5],str2[5];
    int n,x;
    
    int main()
    {
        int cas=0;
        while(~scanf("%d",&n))
        {
            if (n==0) break;
            printf("Case #%d:
    ",++cas);
            priority_queue<node,vector<node>,cmp> A,B,C;
            for(int i=1;i<=n;i++)
            {
                scanf("%s",&ch);
                if (ch[1]=='u')
                {
                   scanf("%s%d",&str1,&x);
                   if (str1[0]=='A') A.push(node(x,i));
                     else B.push(node(x,i));
                } else
                if (ch[1]=='o')
                {
                    scanf("%s",&str1);
                    if(str1[0]=='A')
                    {
                        if(!A.empty()) {printf("%d
    ",A.top().k); A.pop();}
                          else {printf("%d
    ",C.top().k); C.pop();}
                    }  else
                    {
                        if(!B.empty()) {printf("%d
    ",B.top().k); B.pop();}
                          else {printf("%d
    ",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 0;
    }
  • 相关阅读:
    Linux进程间通信之信号量
    C语言文件操作
    Linux多线程同步方法
    Linux进程间通信之共享内存
    关于P2P下载的思考
    学习Jxta,初窥门径
    庆祝一下,基于JXTA的P2P文件共享传输文件测试成功。
    关于java nio在windows下实现
    Java aio和JSR 203
    关于文件目录的P2P共享问题
  • 原文地址:https://www.cnblogs.com/stepping/p/7169052.html
Copyright © 2020-2023  润新知