日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第18届浙江大学校赛 Mergeable Stack

發(fā)布時間:2025/3/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第18届浙江大学校赛 Mergeable Stack 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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:

  • 1?s?v: Push the value??onto the top of the?-th stack.

  • 2?s: Pop the topmost value out of the?-th stack, and print that value. If the?-th stack is empty, pop nothing and print "EMPTY" (without quotes) instead.

  • 3?s?t: Move every element in the?-th stack onto the top of the?-th stack in order.

    Precisely speaking, denote the original size of the?-th stack by?, and the original size of the?-th stack by?. Denote the original elements in the?-th stack from bottom to top by?, and the original elements in the?-th stack from bottom to top by?.

    After this operation, the?-th stack is emptied, and the elements in the?-th stack from bottom to top becomes?. Of course, if?, this operation actually does nothing.

There are??operations in total. Please finish these operations in the input order and print the answer for every operation of the second type.

Input

There 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.

  • If?, two integers??and??(,?) follow, indicating an operation of the first type.
  • If?, one integer??() follows, indicating an operation of the second type.
  • If?, two integers??and??(,?) follow, indicating an operation of the third type.

It's guaranteed that neither the sum of??nor the sum of??over all test cases will exceed?.

Output

For each operation of the second type output one line, indicating the answer.

Sample Input

2 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 Output

13 12 11 10 EMPTY 14 EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
Submit????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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。