C - Mergeable Stack

ZOJ - 4016

一开始用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(链表实现栈)的更多相关文章

  1. ZOJ 4016 Mergeable Stack 链表

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

  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. Java数据结构——用链表实现栈

    //================================================= // File Name : LinkStack_demo //---------------- ...

  5. Java用链表实现栈和队列

    1.用链表实现栈 package stack; /** * * @author denghb * */ class Link { public long dData; public Link next ...

  6. C Mergeable Stack(list超好用)

    ZOJ  4016 list用法https://www.cnblogs.com/LLLAIH/p/10673068.html 一开始用普通的栈做,超内存,链表模拟栈也没写出来orz.补题发现list超 ...

  7. Java之链表实现栈结构

    package com.wzlove.stack; import java.util.Iterator; import java.util.NoSuchElementException; /** * ...

  8. 基于顺序链表的栈的顺序存储的C风格实现

    头文件: #ifndef _SEQSTACK_H_ #define _SEQSTACK_H_ typedef void SeqStack; //创建一个栈 SeqStack* SeqStack_Cre ...

  9. 基于链式链表的栈链式存储的C风格实现

    链式链表的头文件与CPP文件见前文 头文件: #ifndef _LINKSTACK_H_ #define _LINKSTACK_H_ typedef void LinkStack; //创建一个栈 L ...

随机推荐

  1. SQL Server 2012 手动安装帮助文档+排错

    逆天SQL Server 2012装的不要不要的,最后发现...竟然没帮助文档...汗啊!原来它跟vs一样要自己装帮助文档...好吧,官网一下载,妹的...报错...然后就让我们还原这个安装过程以及逆 ...

  2. SQL Server的实例恢复解析

    同Oracle一样,SQL Server在非一致性关闭的时候也会进行实例恢复(Instance Recovery),本文根据stack overflow的文章介绍一些SQL Server实例恢复的知识 ...

  3. 3d max 动作Take 001改名

    问题描述 带动作的Fbx文件导入Unity之后,动作名字为Take 001,如下所示: 在max那边是没有办法改名的,只能在Unity中改名. 方法1 1. 选中动画文件,按Ctrl + D,复制一份 ...

  4. SQL Server数据类型int、bigint、smallint、tinyint对比表

    SQL Server数据类型int.bigint.smallint.tinyint对比表 数据类型 范围 存储 bigint -2^63 (-9,223,372,036,854,775,808) 到 ...

  5. layui 的 GitHub 及 Gitee (码云) 仓库

    GitHub: https://github.com/sentsin/layui/ Gitee:https://gitee.com/sentsin/layui

  6. B - Moo Volume

    Farmer John has received a noise complaint from his neighbor, Farmer Bob, stating that his cows are ...

  7. 用PHP打造一个高性能好用的网站

    用PHP打造一个高性能好用的网站 1. 说到高可用的话要提一下redis,用过的都知道redis是一个具备数据库特征的nosql,正好弥补了PHP的瓶颈,个人认为PHP的 瓶颈在于数据库,像Apach ...

  8. ElasticSearch(三):Java操作ElasticSearch索引之CRUD

    package com.gxy.ESChap01; import java.net.InetAddress; import java.util.HashMap; import java.util.Ma ...

  9. C++ 星号* 与 引用&

    星号 * 1. 声明的时候有*, 表示指针变量 int *p=&a;// '&'的作用就是把a变量在内存中的地址给提取出来 2. * +地址, 表示地址操作符 3. 数字*数字, 表示 ...

  10. 深度学习之Attention Model(注意力模型)

    1.Attention Model 概述 深度学习里的Attention model其实模拟的是人脑的注意力模型,举个例子来说,当我们观赏一幅画时,虽然我们可以看到整幅画的全貌,但是在我们深入仔细地观 ...