ZOJ - 4016 Mergeable Stack 【链表合并】

Given  initially empty stacks, there are three types of operations:

  • s v: Push the value  onto the top of the -th stack.
  • s: Pop the topmost value out of the -th stack, and print that value. If the -th stack is empty, pop nothing and print "EMPTY" (without quotes) instead.
  • s t: Move every element in the -th stack onto the top of the -th stack in order.

    Precisely speaking, denote the original size of the -th stack by , and the original size of the -th stack by . Denote the original elements in the -th stack from bottom to top by , and the original elements in the -th stack from bottom to top by .

    After this operation, the -th stack is emptied, and the elements in the -th stack from bottom to top becomes . Of course, if , this operation actually does nothing.

There are  operations in total. Please finish these operations in the input order and print the answer for every operation of the second type.

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains two integers  and  (), indicating the number of stacks and the number of operations.

The first integer of the following  lines will be  (), indicating the type of operation.

  • If , two integers  and  (, ) follow, indicating an operation of the first type.
  • If , one integer  () follows, indicating an operation of the second type.
  • If , two integers  and  (, ) follow, indicating an operation of the third type.

It‘s guaranteed that neither the sum of  nor the sum of  over all test cases will exceed .

<h4< dd="">Output

For each operation of the second type output one line, indicating the answer.

<h4< dd="">Sample Input

2
2 15
1 1 10
1 1 11
1 2 12
1 2 13
3 1 2
1 2 14
2 1
2 1
2 1
2 1
2 1
3 2 1
2 2
2 2
2 2
3 7
3 1 2
3 1 3
3 2 1
2 1
2 2
2 3
2 3

<h4< dd="">Sample Output

13
12
11
10
EMPTY
14
EMPTY
EMPTY
EMPTY
EMPTY
EMPTY
EMPTY
 1 #include<iostream>
 2 using namespace std;
 3 #include<cstdio>
 4 typedef long long ll;
 5 struct Node{
 6     Node *pt;
 7     ll v;
 8     Node *pre;
 9     Node(){
10         pt = NULL;
11     }
12 };
13 struct Stack{
14     Node *head;
15     Node *tail;
16     Stack(){
17         head=NULL;
18         tail=NULL;
19     }
20     void insert(ll p){
21         if(head==NULL){
22             head = new Node();
23             tail = head;
24             head->v = p;
25             head->pre = NULL;
26         }
27         else{
28             tail->pt = new Node();
29             tail->pt->pre = tail;
30             tail = tail->pt;
31             tail->v = p;
32         }
33     }
34     void pop(){
35         if(tail==head){
36             tail = NULL;
37             head = NULL;
38         }
39         else{
40             Node *temp = tail;
41             tail = tail->pre;
42             delete temp;
43         }
44     }
45     void top(){
46         if(tail!=NULL){
47             printf("%d\n",tail->v);
48             pop();
49         }
50         else
51             printf("EMPTY\n");
52     }
53     void connect(Stack &pt){
54         if(pt.head==NULL)
55             return;
56         if(this->tail==NULL){
57             this->head = pt.head;
58             this->tail =pt.tail;
59             pt.head = NULL;
60             pt.tail = NULL;
61             return;
62         }
63         this->tail->pt = pt.head;
64         pt.head->pre = this->tail;
65         this->tail = pt.tail;
66         pt.head = NULL;
67         pt.tail = NULL;
68     }
69 };
70 Stack *a;
71 void deal(){
72     int n,m;
73     scanf("%d%d",&n,&m);
74     a = new Stack[n+10];
75     for(int i=0;i<m;i++){
76         int p,q,r;
77         scanf("%d",&p);
78         if(p==1){
79             scanf("%d%d",&q,&r);
80             a[q].insert(r);
81         }
82         else if(p==2){
83             scanf("%d",&q);
84             a[q].top();
85         }
86         else if(p==3){
87             scanf("%d%d",&q,&r);
88             a[q].connect(a[r]);
89         }
90     }
91 }
92 int main(){
93     int t;
94     scanf("%d",&t);
95     while(t--)
96         deal();
97     return 0;
98 }

原文地址:https://www.cnblogs.com/xfww/p/8819536.html

时间: 2024-11-12 01:50:31

ZOJ - 4016 Mergeable Stack 【链表合并】的相关文章

ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)

Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given initially empty stacks, there are three types of operations: 1 s v: Push the value onto the top of the -th stack. 2 s: Pop the topmost value out of the -th stack, and print that

ZOJ - 4016 Mergeable Stack (STL 双向链表)

[传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 [题目大意]初始有n个空栈,现在有如下三种操作: (1) 1 s v  即 s.push(v) (2) 2 s 即 s.pop() 输出弹出的元素,如果栈s为空则输出 "EMPTY" (3) 3 s t 把t栈元素全部移到s栈中,使s的尾部与t的首部相连. 现在有若干上述三种类型的操作,遇到操作2则输出相应内容. [题解]由于站的数量n和操作次数

[ZOJ 4016] Mergable Stack

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 直接用栈爆内存,看网上大神用数组实现的,构思巧妙,学习了! AC代码: /* * 用数组实现栈的一些操作 */ #include <cstdio> #include <cstring> using namespace std; const int maxn = 300005; int arr[maxn]; //存放所有的数据 //top代表栈

ZOJ 3210 A Stack or A Queue? (I)

A Stack or A Queue? Time Limit: 1 Second      Memory Limit: 32768 KB Do you know stack and queue? They're both important data structures. A stack is a "first in last out" (FILO) data structure and a queue is a "first in first out" (FIF

将两个有序链表合并

题目:已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序.(保留所有结点,即便大小相同) 循环实现: 1.重新申请一个头结点,使用指针p指向他,每新加一个结点,就将指针p后移一位,即指针p永远指向新链表的尾结点 2.由于所用链表第一个结点不赋值,因此指针需要开始从头结点的下一个结点开始判断,如果两个指针都为非空,将data域较小的指针连在新链表的末尾 3.当两个指针有一个到达链表的结尾时,将没有到达末尾的链表连接到新链表之后 递归实现: 1.函数返回条件是有一个链表结

双链表&amp;链表合并&amp;多项式相加算法

//单链表的合并 //链表合并 //两个链表必须是有序的 #define Maxsize 5 typedef  int elemtype; typedef struct linklist { elemtype data; struct linklist *next; }Linklist; //建立链表1 Linklist *CreateList1 () { int i,data ; Linklist *head, *p, *q; head=p=(Linklist  *)malloc(sizeof

zoj 3210 A Stack or A Queue? (数据结构水题)

 A Stack or A Queue? Time Limit: 1 Second      Memory Limit: 32768 KB Do you know stack and queue? They're both important data structures. A stack is a "first in last out" (FILO) data structure and a queue is a "first in first out" (

单链表合并排序实现

原题是要实现两个已排序的单链表合并后还是已排序,但我在网上查了很多都无法直接实现.对于初学者给个算法是没多大用的,下面给出完整代码.主要思路就是先接尾再排序.而一般书是直接开始分情况if...else if...else嵌套排序.比较复杂. /*关键:两个有序单链表的合并:其实本程序可以实现任意两个单链表的合并排序,思想就是 *1.建两个链表2.合并两个链表3.对合并后的链表排序4.打印 *关键函数:linkDList 直接连接两个链表;selectsort 单链表的选择排序*/ #define

zoj 3790 Consecutive Blocks(链表重点是思想)

Consecutive Blocks Time Limit: 2 Seconds      Memory Limit: 65536 KB There are N (1 ≤ N ≤ 105) colored blocks (numbered 1 to N from left to right) which are lined up in a row. And the i-th block's color is Ci (1 ≤ Ci ≤ 109). Now you can remove at mos