[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代表栈顶元素在arr中的下标,
//Next代表栈顶元素下一个元素在arr中的下标,用于实现在栈顶元素被抛出后,栈顶新的指向,
//bottom代表栈低元素在arr中的位置
int top[maxn], Next[maxn], bottom[maxn];

int main() {
    int T;
    scanf("%d", &T);
    int n, q;
    int op, cnt;
    int s, v, t;
    while (T--) {
        cnt = 0;
        memset(top, 0, sizeof(top));
        memset(Next, 0, sizeof(Next));
        memset(bottom, 0, sizeof(bottom));
        scanf("%d%d", &n, &q);
        while (q--) {
            scanf("%d", &op);
            if (op == 1) {
                scanf("%d%d", &s, &v);
                arr[++cnt] = v;
                if (bottom[s] == 0) {  //如果原先栈为空,新加入的第一个元素将成为栈底元素
                    bottom[s] = cnt;
                }
                //更新栈顶元素和栈顶的下一个元素
                Next[cnt] = top[s]; //也可写作Next[top[s]] = top[s];
                top[s] = cnt;
            } else if (op == 2) {
                scanf("%d", &s);
                if (top[s]) {
                    printf("%d\n", arr[top[s]]); //抛出栈顶元素,注意,top存放的是栈顶元素在arr中的下标
                    top[s] = Next[top[s]]; //栈顶元素重定向
                    if (top[s] == 0) {  //若栈为空,则栈顶和栈底都置为0,容易忽略这个判断
                        bottom[s] = 0;
                    }
                } else {
                    printf("EMPTY\n");
                }
            } else if (op == 3) {
                scanf("%d%d", &s, &t);
                if (top[t]) {
                    if (bottom[s] == 0) {  //如果第s个栈为空,则s的栈底就是t的栈底
                        bottom[s] = bottom[t];
                    }
                    //t的栈底元素的下一个为s的栈顶元素,这里不用在重定向s栈顶元素的下一个元素,因为t已经指定过了
                    Next[bottom[t]] = top[s];
                    top[s] = top[t];
                }
                top[t] = bottom[t] = 0; //把第t个栈置为空,容易忽略
            }
        }
    }
    return 0;
}

我写的代码,不能AC

/*
 * Memory Limit Exceeded
 */
#include <stack>
#include <cstdio>

using namespace std;

const int maxn = 300005;

stack<int> arr[maxn];

int maze[maxn];

int x, y, z;

void solve_push(int y, int z) {
    arr[y].push(z);
}

void solve_top(int y) {
    if (!arr[y].empty()) {
        int x = arr[y].top();
        arr[y].pop();
        printf("%d\n", x);
    } else {
        printf("EMPTY\n");
    }
}

void solve_move(int y, int z) {
    stack<int> temp;
    int x;
    while (!arr[z].empty()) {
        x = arr[z].top();
        arr[z].pop();
        temp.push(x);
    }
    while (!temp.empty()) {
        x = temp.top();
        temp.pop();
        arr[y].push(x);
    }
}

int main() {
    int t;
    int n, q;
    int flag;

    scanf("%d", &t);
    while (t--) {
        flag = 0;
        scanf("%d%d", &n, &q);
        for (int i = 0; i < q; i++) {
            scanf("%d", &x);
            if (x == 1) {
                scanf("%d%d", &y, &z);
                maze[flag++] = y;
                solve_push(y, z);
            }
            if (x == 2) {
                scanf("%d", &y);
                maze[flag++] = y;
                solve_top(y);
            }
            if (x == 3) {
                scanf("%d%d", &y, &z);
                maze[flag++] = y;
                maze[flag++] = z;
                solve_move(y, z);
            }
        }
        for (int i = 0; i < flag; i++) {
            while (!arr[maze[flag]].empty()) {
                arr[maze[flag]].pop();
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/youpeng/p/10778218.html

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

[ZOJ 4016] Mergable Stack的相关文章

ZOJ - 4016 Mergeable Stack 【链表合并】

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 value. If the -th stack is empty, pop nothing and print "EMPTY&q

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

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

ZOJ 3210: A Stack or A Queue?

A Stack or A Queue? ///@author Sycamore, ZJNU ///@date 2017-02-09  #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int T, N; cin >> T; while (T--) { cin >> N; vector<int>v(N); f

ZOJ 3210 A Stack or A Queue ? 水

C - A Stack or A Queue? Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%lld & %llu SubmitStatus Description Do you know stack and queue? They're both important data structures. A stack is a "first in last out" (FILO) data struc

stack+DFS ZOJ 1004 Anagrams by Stack

题目传送门 1 /* 2 stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 3 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <stack> 9 #include <cmath> 10 #include <cstring> 11 #inc

ZOJ 1004 Anagrams by Stack

Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一个字符串转变成目标字符串的操作,要求输出全部的可能操作组合. 思路:利用深度优先的搜索思路,对于每一个状态都有入栈和出栈两种可能的操作,由于要求按字典序输出,每次先考虑入栈再考虑出栈.即"能入就入,不能入考虑是否能退,随后返回上一步". 下面贴代码: 1 //Problem Name: A