zoukankan      html  css  js  c++  java
  • 数据结构 下车的顺序 (STL+stack)

    Description

    很多高校都有两个或多个校区,为了方便教师和学生往返于 这两个校区,学校在两个校区之间开通了校车。用于通勤的校车只有一个门,上、下车均需要通过此门,且校车的容量上限为c。车内过道很窄,只能容纳一个人通 过,为了运送更多的人,车内中间过道上设有活动、可折叠的坐椅,如果有人站在或者坐在中间过道上,任何人都无法通过,是所谓一夫当关万夫莫开。在非常拥挤 的情况下,司机通常让先上的人尽量坐在后排位置,这样从后往前一排排坐过来,最先上的人坐最后面,最后上的在最前面。当车到达目的地后,下车时,只能是靠 近车门的人先下,这样一排排往后,最后一排的人最后下车。请编程模拟乘坐通勤车的情景:先上的人最后下来。每个乘客用一个编号来表示,用户给出一组乘客上 车的编号(即乘客的等车次序),程序输出下车编号顺序。

    Input

    输入第一行为测试数据组数。每组测试数据 第1行输入两个整数n(1<=n<=100)、c(1<=c<=20000)分别代表停车的次数和校车容量,接下来有n行,每一 行第一个数m表示有人上车还是下车(0 <= m <= 200):(1)当m=0时,后面紧跟着输入一个非负整数k,表示此时有k个人下车;(2)当m>0时,后面输入m个正整数,表示等待上车的人的编 号,可能有人因车满而无法上车。

    Output

    对每组测试数据,第1行输出数据组数,然后对每一个m为0输出一行k个数的编号(即每次输出都有谁下车了),每两个数中间用空格隔开。

    Sample Input

    1
    4 10
    6 1 2 3 4 5 6
    0 3
    2 7 8
    0 3

    Sample Output

    Case #1:
    6 5 4
    8 7 3

    HINT

    考察知识点:栈, 时间复杂度O(n),空间复杂度O(n)

    Append Code

    析:这是一个很典型的栈操作,因为是先进后出,直接用栈模拟就好,进来人,就压入栈内,下车就弹出。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <list>
    #define ALL(x) x.begin(),x.end()
    #define INS(x) inserter(x,x.begin())
    #define frer freopen("in.txt", "r", stdin)
    #define frew freopen("out.txt", "w", stdout)
    using namespace std;
     
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 5;
    const int mod = 1e9 + 7;
    const char *mark = "+-*";
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    stack<int> bus;
     
    int main(){
        int T;  cin >> T;
        for(int kase = 1; kase <= T; ++kase){
            printf("Case #%d:
    ", kase);
            scanf("%d %d", &n, &m);
            int x, y;
            while(!bus.empty())  bus.pop();
            for(int i = 0; i < n; ++i){
                scanf("%d", &x);
                if(!x){
                    scanf("%d", &y);
                    for(int j = 0; j < y && bus.size() > 0; ++j){
                        if(!j){  printf("%d", bus.top());  bus.pop(); }
                        else { printf(" %d", bus.top());  bus.pop(); }
                    }
                    printf("
    ");
                }
                else{
                    for(int j = 0; j < x; ++j){
                        scanf("%d", &y);
                        if(bus.size() >= m)  continue;
                        bus.push(y);
                    }
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    解决在火狐中不支持onreadystatechange的方法
    jquery ajax与分页的实现(mvc下)
    通过ChekBox设置EditText内容是否呈现密码显示
    Android自定义dialog
    Android Intent笔记
    启动另外的一个应用程序的Activity(三种方式)(转载)
    Android代码中声明一个layout
    在Linux下创建函数库
    五分钟讲清楚UML
    QT中使用QProcess启用外部程序
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5875517.html
Copyright © 2011-2022 走看看