题目链接: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;
}

又看到网上一个用list实现的

#include <cstdio>
#include <list> using namespace std;
const int maxn = 3e5 + 5;
list<int> l[maxn]; int main() {
int n, t, q;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
l[i].clear();
while (q--) {
int op, s, v, t;
scanf("%d", &op);
if (op == 1) {
scanf("%d%d", &s, &v);
l[s].push_back(v);
} else if (op == 2) {
scanf("%d", &s);
if (l[s].empty()) printf("EMPTY\n");
else {
printf("%d\n", l[s].back());
l[s].pop_back();
}
} else if (op == 3) {
scanf("%d%d", &s, &t);
l[s].splice(l[s].end(), l[t]); //splice 直接进行合并不进行排序,merge默认升序合并排序
}
}
}
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;
}

[ZOJ 4016] Mergable Stack的更多相关文章

  1. ZOJ - 4016 Mergeable Stack 【LIST】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 题意 模拟栈的三种操作 第一种 push 将指定元素压入指 ...

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

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given initially empty stacks, ther ...

  3. ZOJ 4016 Mergeable Stack(栈的数组实现)

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given  initially empty stacks, the ...

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

    [传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 [题目大意]初始有n个空栈,现在有如下三种操作: (1) ...

  5. ZOJ 4016 Mergeable Stack 链表

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given  initially empty stacks, the ...

  6. ZOJ 4016 Mergeable Stack(from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    模拟题,用链表来进行模拟 # include <stdio.h> # include <stdlib.h> typedef struct node { int num; str ...

  7. Mergeable Stack ZOJ - 4016(list)

    ZOJ - 4016 vector又T又M list是以链表的方式存储的 是一个双向链表 元素转移操作中,不能一个个遍历加入到s中,list独有的splic函数可以在常数时间内实现合并(并删除源lis ...

  8. 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? ...

  9. 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? Th ...

随机推荐

  1. SQL行转列与列转行(转)

    原文: http://blog.csdn.net/jx_870915876/article/details/52403472 add by zhj: 本文是以MySQL为例说明的,但其实它适用于所有关 ...

  2. react-devtool 消息处理渲染 源码理解

    react-devtools 有chrome插件版,但在chrome 插件下的通信调试不够透明,且chrome 的插件特别是开发工具界面几乎无法调试. 看到了react-devtools 也提供ele ...

  3. tensorflow 基本内容

    tensorflow的结构 1.使用图(graphs)来表示计算任务 2.在被称之为会话(Session)的上下文(context)中执行图 3.使用tensor表示数据 4.通过变量(Variabl ...

  4. 设置光标聚焦输入框(EditText)并弹出软键盘(在适配器中设置)

    参考代码: public void setFocusEditTextAndShowSoftInput(final EditText editText){ editText.setFocusable(t ...

  5. Swagger插件netcore配置

    步骤一. Nuget Packages安装,使用程序包管理器控制台,安装命令:Install-Package Swashbuckle.AspNetCore -Pre 步骤二. 在Startup 文件中 ...

  6. Android系统架构与系统源码目录

    前言 技术博客终于可以恢复正常的更新速度了,原因是我编写的进阶书籍的初稿已经完成,窃以为它将会是Android应用书籍中最有深度的一本,可以说是<Android开发艺术探索>的姊妹篇.在这 ...

  7. react创建项目很慢,最后提示fetch failed的解决方法

    $ cnpm install -g create-react-app //创建react全局变量 $ create-react-app my-app //创建一个react项目 国内使用 npm 速度 ...

  8. REDHAT YUM本地源的搭建和使用

    yum源一般分为两种,本地yum源和本地网络yum源,前者是通过文件提供安装包,后者是通过网络下载安装包: 由于Redhat7.3的yum源需要注册付费,所以往往会出现下载yum源安装包失败,如下图: ...

  9. 深入理解Java虚拟机7-chap10-11-斗者4星

    一.编译期优化 1.JIT编译器在运行期的优化过程对程序运行很重要,而编译期优化过程对于程序编码关系更密切 2.Javac编译器编译过程 解析与填充符号表过程:词法语法分析.填充符号表 插入式注解处理 ...

  10. 解决页面使用ifrmae后,在session失效后登录页面在子页面中显示(子窗体出现父窗体)

    在登录页面中添加js判断当前页面是否是父页面,诺不是则父页面跳转至登录页面. <script type="text/javascript"> //解决登录后多个父窗体问 ...