Mergeable Stack(链表实现栈)
C - Mergeable Stack
一开始用stl中内置的栈来写,其中第三个操作,我先复制到一个数组,再将其倒给另一个栈
这个方法有两个错误的地方:
1.栈在内存很大需要扩容时,内存会成倍增长,解决办法是提前规定每个栈的大小,但这样还是不适用于这题
2.如果每次都用一个数组来过度,时间复杂度是O(N*N)
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=3*1e5+5;
struct node
{
node *nex;
int num;
};
node *head[maxn];
node *tail[maxn];
int num[maxn];
void add(int s,int v)
{
node *chan=head[s];
head[s]=new node;
if(tail[s]==NULL)
{
tail[s]=head[s];
}
head[s]->num=v;
head[s]->nex=chan;
}
int main()
{
int T,s,v,t,q,n,comd;
cin>>T;
for(int i=1;i<=T;i++)
{
scanf("%d %d",&n,&q);
for(int j=1;j<=n;j++)
head[j]=tail[j]=NULL;
for(int j=1;j<=q;j++)
{
scanf("%d",&comd);
if(comd==1)
{
scanf("%d %d",&s,&v);
add(s,v);
}
else if(comd==2)
{
scanf("%d",&s);
if(head[s]==NULL)
printf("EMPTY\n");
else
{
printf("%d\n",head[s]->num);
head[s]=head[s]->nex;
if(head[s]==NULL)tail[s]=NULL;
}
}
else if(comd==3)
{
scanf("%d %d",&s,&t);
if(tail[t]!=NULL&&tail[s]!=NULL)
{
tail[t]->nex=head[s];
head[s]=head[t];
tail[t]=NULL;
head[t]=NULL;
}
else if(tail[s]==NULL)
{
head[s]=head[t];
tail[s]=tail[t];
head[t]=NULL;
tail[t]=NULL;
}
}
} }
return 0;
}
Mergeable Stack(链表实现栈)的更多相关文章
- ZOJ 4016 Mergeable Stack 链表
Mergeable Stack Time Limit: 2 Seconds Memory Limit: 65536 KB Given initially empty stacks, the ...
- ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)
Mergeable Stack Time Limit: 2 Seconds Memory Limit: 65536 KB Given initially empty stacks, ther ...
- ZOJ 4016 Mergeable Stack(栈的数组实现)
Mergeable Stack Time Limit: 2 Seconds Memory Limit: 65536 KB Given initially empty stacks, the ...
- Java数据结构——用链表实现栈
//================================================= // File Name : LinkStack_demo //---------------- ...
- Java用链表实现栈和队列
1.用链表实现栈 package stack; /** * * @author denghb * */ class Link { public long dData; public Link next ...
- C Mergeable Stack(list超好用)
ZOJ 4016 list用法https://www.cnblogs.com/LLLAIH/p/10673068.html 一开始用普通的栈做,超内存,链表模拟栈也没写出来orz.补题发现list超 ...
- Java之链表实现栈结构
package com.wzlove.stack; import java.util.Iterator; import java.util.NoSuchElementException; /** * ...
- 基于顺序链表的栈的顺序存储的C风格实现
头文件: #ifndef _SEQSTACK_H_ #define _SEQSTACK_H_ typedef void SeqStack; //创建一个栈 SeqStack* SeqStack_Cre ...
- 基于链式链表的栈链式存储的C风格实现
链式链表的头文件与CPP文件见前文 头文件: #ifndef _LINKSTACK_H_ #define _LINKSTACK_H_ typedef void LinkStack; //创建一个栈 L ...
随机推荐
- EF CodeFirst使用Nuget更新数据库
常用命令: 1.开启迁移 Enable-Migrations -EnableAutomaticMigrations 2.添加一条迁移记录 Add-Migration AddMigration001 3 ...
- window scoop 修改默认安装路径
1.运行powershell [environment]::setEnvironmentVariable('SCOOP_GLOBAL','F:\GlobalScoopApps','Machine') ...
- 解决Eclipse点击运行后控制台不能自动弹出的问题
解决方案: 选择Window-->Preferences-->Run.Debug-->Console 勾选"Show when program writest to sta ...
- May 31. 2018 Week 22nd Thursday
The good seaman is known in bad weather. 惊涛骇浪,方显英雄本色. As we all know, the true worth of a person is ...
- nginx + uwsgi 部署 Django+Vue项目
nginx + uwsgi 部署 Django+Vue项目 windows 本地 DNS 解析 文件路径 C:\Windows\System32\drivers\etc 单机本地测试运行方式,调用dj ...
- Java 8 新特性:1-函数式接口
(原) Java 8 新特性1-函数式接口 Lambda表达式基本结构: (param1,param2,param3) -> {代码块} Lambda表达式结构: (type1 arg1,typ ...
- [1] 从零开始 TensorFlow 学习
计算图的基本概念 TensorFlow 的名字中己经说明了它最重要的两个概念一一Tensor 和 Flow Tensor: 张量(高阶数组,矩阵为二阶张量,向量为一阶张量,标量为零阶张量) Flow: ...
- jquery懒加载插件 jquery_lazyload 下载
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code https://pan.baidu.com/s/1UbOeyL_AaSNN_KMA4M ...
- 001学习Python的ABC模块(转)
http://yansu.org/2013/06/09/learn-Python-abc-module.html 1.abc模块作用 Python本身不提供抽象类和接口机制,要想实现抽象类,可以借助a ...
- Spring Security(八):2.4.3 Project Modules
In Spring Security 3.0, the codebase has been sub-divided into separate jars which more clearly sepa ...