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 ...
随机推荐
- sqlserver中分区函数 partition by与 group by 区别 删除关键字段重复列
partition by关键字是分析性函数的一部分,它和聚合函数(如group by)不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录, partition by ...
- c# 设置MdiClient窗体的背景图片
在窗体的InitializeComponent();方法后面添加下面的代码. MdiClient MC = new MdiClient(); MC.Name = "MdiClientForm ...
- Session变量在PHP中的使用
PHP session 变量用于存储有关用户会话的信息,或更改用户会话的设置.Session 变量保存的信息是单一用户的,并且可供应用程序中的所有页面使用. PHP Session 变量 当您运行一个 ...
- IntelliJ IDEA 导入Spring源码
第一步: 使用git 拉取代码 git 命令: git init //创建git仓库 git clone https://github.com/spring-projects/spring-f ...
- 【Teradata】块压缩(ferret工具)
多值压缩(MVC) Enhanced Multi-Value Compression (MVC) or Value-List Compression• Compress VARCHAR, VARBYT ...
- C# 基础知识之 Unix 时间戳转换
unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒. /// 时间戳转为C#格式时间 private DateTime GetTime(string timeSt ...
- 使用IntelliJ IDEA和Maven管理搭建Web开发环境(以Spring MVC为例)(一)
前言:原来一直使用MyEclipse,换工作后,新公司使用IDEA,初识IDEA发现,哇,它的快捷键可真多啊,但是一路用下来,觉得非常的好用,特别是利用Maven管理,那简直叫一个爽.当然笔者在使用过 ...
- flask的migrate
https://blog.csdn.net/kevin_qq/article/details/51777190 这个方法可以: https://www.cnblogs.com/caicairui/p/ ...
- hystrix
<servlet> <display-name>HystrixMetricsStreamServlet</display-name> <servlet-nam ...
- Linux的常见问题解答和管理技巧
Linux的常见问题解答和管理技巧 一. 如何建立多用户 提醒大家一句,别一直使用root用户,因为root用户在系统中有着至高无上的权力,一不小心就可能破坏系统.比如我们想删除/temp目录下的文件 ...