【The 18th Zhejiang University Programming Contest Sponsored by TuSimple - C】Mergeable Stack(list)

原创 2018年04月15日 10:09:56

链接:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5734
题意:就是一个模拟栈的题,不同的是有一个操作是把第s个栈中的所有元素全部剪切到第t个栈。 栈的个数n和操作次数p最大为3e5.

分析:当时就直接用stack,MLE,改vector来模拟,也是MLE,然后想到的是手动建立栈(用数组模拟)

https://blog.csdn.net/codeswarrior/article/details/79852136
思路是把所有数字都存在一个数组a中,然后用三个数组top,Next,bottom分别记录栈顶栈底和栈顶下一个数字在a数组的下标数字,这样最多只用四个3e5的数组不会超内存也不回超时

听说有STL中的list 用起来像暴力? 没错, 可惜没学会

【list】

#include <bits/stdc++.h>
using namespace std;

#define rep(i,a,n) for(int i=a;i<n;i++)
#define mem(a,n) memset(a,n,sizeof(a))
#define pb push_back
#define IO ios::sync_with_stdio(false);
typedef long long ll;
const int N=3e5+5;
const double eps=1e-4;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1}; ///上下左右

list<int>v[N];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,q;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++) v[i].clear();
        while(q--)
        {
            int x,s,val,t;
            scanf("%d",&x);
            if(x==1)
            {
                scanf("%d%d",&s,&val);
                v[s].pb(val);
            }
            else if(x==2)
            {
                scanf("%d",&t);
                if(v[t].empty()) puts("EMPTY");
                else
                {
                    printf("%d\n",v[t].back());
                    v[t].pop_back();
                }
            }
            else
            {
                scanf("%d%d",&s,&t);
                v[s].splice(v[s].end(),v[t]);
            }
        }
    }
    return 0;
}
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/feng_zhiyu/article/details/79946975

The 17th Zhejiang University Programming Contest Sponsored by TuSimple 浙大校赛C题 How Many Nines

#include #include #include #include using namespace std;bool check(int y) { if(y%4 == 0 && y ...
  • litmxs
  • litmxs
  • 2017-04-09 17:18:39
  • 280

ZOJ-The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple-C

ACM模版描述 题解这个题的难点是翻译,翻译好了,一遍 AC。首先输入 T,表示数据组数。 接着输入 n 和 q 表示 n 次询问与 q 次关系。 然后一个 c 表示可能出现的名字,接着就是 c...
  • f_zyj
  • f_zyj
  • 2017-04-23 23:40:10
  • 275

The 15th Zhejiang University Programming Contest

A-Find the Spy(zoj 3860) Find the Spy Time Limit: 2 Seconds      Memory Limit: 65536 KB W...
  • qq243887
  • qq243887
  • 2015-04-14 19:57:09
  • 744

The 11th Zhejiang University Programming Contest C G J

Chinese ZodiacGaussian PrimeJudge Internal Error只贴代码,不解释。。T T 。。。I题木有搞出来。。。哎。。。Final Rank 99 T T 。。C...
  • zxy_snow
  • zxy_snow
  • 2011-04-03 20:13:00
  • 855

The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple C题What Kind of Fri

What Kind of Friends Are You? Time Limit: 1 Second      Memory Limit: 65536 KB Japari Park is ...
  • qq_34826781
  • qq_34826781
  • 2017-04-25 21:15:02
  • 264

ZOJ 142 - The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - B

Problem PreparationTime Limit: 1 Second Memory Limit: 65536 KBIt’s time to prepare the problems...
  • linglian0522
  • linglian0522
  • 2017-04-22 22:02:34
  • 880

The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple(A+B+C+D)

Cooking CompetitionTime Limit: 1 Second Memory Limit: 65536 KB “Miss Kobayashi’s Dragon Maid”...
  • qq_33183401
  • qq_33183401
  • 2017-04-23 01:11:49
  • 691

ZOJ 142 - The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - D

Let’s ChatTime Limit: 1 Second Memory Limit: 65536 KBACM (ACMers’ Chatting Messenger) is a famo...
  • linglian0522
  • linglian0522
  • 2017-04-22 22:09:03
  • 510

ZOJ3953 Intervals 【贪心 双线程活动分配问题】

Intervals Time Limit: 1 Second Memory Limit: 65536 KB Special Judge Chiaki has n intervals...
  • LuRiCheng
  • LuRiCheng
  • 2017-04-11 22:07:24
  • 546

【ZOJ3952 The 17th Zhejiang University Programming Contest E】【简单构造 模拟 or汇编】Fibonacci Sequence Chicken

Fibonacci Sequence Chicken Edition Time Limit: 1 Second      Memory Limit: 65536 KB      Special ...
  • snowy_smile
  • snowy_smile
  • 2017-04-12 23:21:14
  • 519
收藏助手
不良信息举报
您举报文章:【The 18th Zhejiang University Programming Contest Sponsored by TuSimple - C】Mergeable Stack(list)
举报原因:
原因补充:

(最多只允许输入30个字)