zoj4016用list去模拟栈

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.

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.

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

OutputFor 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

如果当时我们很熟悉stl模板的话,就很轻易出了这道题了,注意最后一个元素必须用a.back()而不是输出a.end()

https://www.cnblogs.com/scandy-yuan/archive/2013/01/08/2851324.html 这是list详细应用

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <list>
  4. using namespace std;
  5. list<long long >a[300009];
  6. int main()
  7. {
  8.     int t;
  9.     scanf(“%d”,&t);
  10.     while(t–)
  11.     {
  12.         int n,q,op,s,v,i,c;
  13.         scanf(“%d%d”,&n,&q);
  14.         for(i=1; i<=n; i++)
  15.         {
  16.             a[i].clear();
  17.         }
  18.         while(q–)
  19.         {
  20.             scanf(“%d”,&op);
  21.             if(op==1)
  22.             {
  23.                 scanf(“%d%lld”,&s,&v);
  24.                 a[s].push_back(v);
  25.             }
  26.             else if(op==2)
  27.             {
  28.                 scanf(“%d”,&s);
  29.                 if(a[s].empty())
  30.                 {
  31.                     printf(“EMPTY\n”);
  32.                 }
  33.                 else
  34.                 {
  35.                     printf(“%lld\n”,a[s].back());
  36.                     a[s].pop_back();
  37.                 }
  38.             }
  39.             else if(op==3)
  40.             {
  41.                 scanf(“%d%d”,&s,&c);
  42.                 a[s].splice(a[s].end(),a[c]);
  43.             }
  44.         }
  45.     }
  46. }