The 18th Zhejiang University Programming Contest Sponsored by TuSimple
第18届浙江大学校赛的c题
解析:起先是用stack写的模拟,但是先是爆内存,然后在爆时间,结束后才知道,原来Stl在每次 变长内存空间的时候
是2倍的指数增长的,所以就是他的一个测试的样例就是刚好会使stl两倍的增长。时间爆是stack每次替换元素会o(n)。
这时候通常就是要手写模拟了,开数组模拟。但是有一个东西特别,list,这个stl是指针的有关操作,就是很内存还有时间就是
比较省,一步一步的增加的,时间也是,不会和stack指数增长。以下是代码:(list的相关函数晚上)
152 - The 18th Zhejiang University Programming Contest Sponsored by TuSimple - C
Given initially empty stacks, there are three types of operations:
There are operations in total. Please finish these operations in the input order and print the answer for every operation of the second type. InputThere are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case: The first line contains two integers and (), indicating the number of stacks and the number of operations. The first integer of the following lines will be (), indicating the type of operation.
It's guaranteed that neither the sum of nor the sum of over all test cases will exceed . OutputFor each operation of the second type output one line, indicating the answer. Sample Input2 2 15 1 1 10 1 1 11 1 2 12 1 2 13 3 1 2 1 2 14 2 1 2 1 2 1 2 1 2 1 3 2 1 2 2 2 2 2 2 3 7 3 1 2 3 1 3 3 2 1 2 1 2 2 2 3 2 3 Sample Output13 12 11 10 EMPTY 14 EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY |
Copyright @ 2001-2018, Zhejiang University ACM/ICPC Team, All rights reserved.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<list>
#include<algorithm>
using namespace std;
const int maxn=300005;
list<int> l[maxn];
int main()
{
int n,q,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&q);
for(int i=1; i<n+1; i++)l[i].clear();
while(q--)
{
int op;
scanf("%d",&op);
if(op==1)
{
int s,v;
scanf("%d%d",&s,&v);
l[s].push_back(v);
}
if(op==2)
{
int s;
scanf("%d",&s);
if(l[s].empty())puts("EMPTY");
else
{
printf("%d\n",l[s].back());
l[s].pop_back();
}
}
if(op==3)
{
int s,t;
scanf("%d%d",&s,&t);
l[s].splice(l[s].end(),l[t]);
}
}
}
return 0;
}