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. 消除Warning: Using a password on the command line interface can be insecure的提示

    最近在部署Zabbix时需要用脚本取得一些MySQL的返回参数,需要是numberic格式的,但是调用脚本时总是输出这一句: Warning: Using a password on the comm ...

  2. c/c++ 智能指针 unique_ptr 使用

    智能指针 unique_ptr 使用 和shared_ptr不同,可以有多个shared_ptr指向同一个内存,只能有1个unique_ptr指向某个内存.因此unique_ptr不支持普通的拷贝和赋 ...

  3. Ajax的一个实例及代码

    这是用ajax做的一个小小的应用!当选择menu1的时候,会出来menu里面所有的内容.同理对于menu2.多的不说,代码如下: 首先是inner.html文件 <html><hea ...

  4. Session变量在PHP中的使用

    PHP session 变量用于存储有关用户会话的信息,或更改用户会话的设置.Session 变量保存的信息是单一用户的,并且可供应用程序中的所有页面使用. PHP Session 变量 当您运行一个 ...

  5. 关于phpstorm ftp目录乱码

    关于IIS FTP服务器汉字文件目录乱码问题:一般来说,IIS 服务器编码默认为GBK,而你的目录可能是UTF-8,将phpstorm的远程连接设置为GBK就OK了.记住服务器的编码,文件的编码要统一

  6. java继承-super的用法

    继承的特点: 1.子类继承了父类的成员 2.具有层次结构 继承的优点: 1.代码重用 2.父类字段和方法可用于子类 3.从抽象到具体形成类的继承体系 4.可以轻松自定义子类 this:当前对象的引用 ...

  7. NGINX Load Balancing – TCP and UDP Load Balancer

    This chapter describes how to use NGINX Plus and open source NGINX to proxy and load balance TCP and ...

  8. 关于this的理解

    var o = { a:10, b:{ a:12, fn:function(){ console.log(this.a); //undefined console.log(this); //windo ...

  9. (转)Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例

    http://www.ityouknow.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 这篇文章介绍如何使用 Jpa 和 ...

  10. sqlite线程模式的设置

    (1)编译阶段 这几种模式可以通过参数SQLITE_THREADSAFE在编译阶段指定,可以取值0,1,2,默认是1.这三种取值的含义如下: 0:单线程模式,即内部不做mutex保护,多线程运行sql ...