The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack
题目链接
题意:
题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!!
代码:
#include <stdio.h> #include <iostream> #include <vector> #include <deque> #include <list> #define IO ios::sync_with_stdio(0);\ cin.tie(0);cout.tie(0); using namespace std; const int N = 3*100000+5; list <int > q[N]; int main() { int T; scanf("%d",&T); //cin >> T; while(T--) { int n,q1; scanf("%d%d",&n,&q1); for(int i = 1; i <= n; i++) q[i].clear(); while(q1--) { int op,s,v,t; scanf("%d",&op); if(op==1) { scanf("%d%d",&s,&v); q[s].push_back(v); } else if(op==2) { scanf("%d",&s); if(q[s].empty()) { //cout << "EMPTY" << endl; printf("%s\n", "EMPTY"); } else { //cout << q[s].back() <<endl; printf("%d\n",q[s].back()); q[s].pop_back(); } } else if(op == 3) { scanf("%d%d",&s,&t); q[s].splice(q[s].end(),q[t]); //将s,t合并,并清除t中元素 //q[t].clear(); } } } return 0 ; }
宝剑锋从磨砺出 梅花香自苦寒来