第18届浙江大学校赛 Mergeable Stack
The 18th Zhejiang University Programming Contest Sponsored by TuSimple
第18屆浙江大學(xué)校賽的c題
解析:起先是用stack寫的模擬,但是先是爆內(nèi)存,然后在爆時間,結(jié)束后才知道,原來Stl在每次 變長內(nèi)存空間的時候
是2倍的指數(shù)增長的,所以就是他的一個測試的樣例就是剛好會使stl兩倍的增長。時間爆是stack每次替換元素會o(n)。
這時候通常就是要手寫模擬了,開數(shù)組模擬。但是有一個東西特別,list,這個stl是指針的有關(guān)操作,就是很內(nèi)存還有時間就是
比較省,一步一步的增加的,時間也是,不會和stack指數(shù)增長。以下是代碼:(list的相關(guān)函數(shù)晚上)
| 152 - The 18th Zhejiang University Programming Contest Sponsored by TuSimple - CMergeable Stack Time Limit:?2 Seconds ?????Memory Limit:?65536 KB 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 3Sample Output13 12 11 10 EMPTY 14 EMPTY EMPTY EMPTY EMPTY EMPTY EMPTYSubmit????Status |
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;}
總結(jié)
以上是生活随笔為你收集整理的第18届浙江大学校赛 Mergeable Stack的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最近比较火的一款字节产品
- 下一篇: 关于并查集的个人再次的理解