团体天梯练习 L2-037 包装机

发布时间 2023-04-20 10:58:38作者: Amαdeus

L2-037 包装机

一种自动包装机的结构如图 1 所示。首先机器中有 \(N\) 条轨道,放置了一些物品。轨道下面有一个筐。当某条轨道的按钮被按下时,活塞向左推动,将轨道尽头的一件物品推落筐中。当 \(0\) 号按钮被按下时,机械手将抓取筐顶部的一件物品,放到流水线上。图 2 显示了顺序按下按钮 $3、2、3、0、1、2、0 $ 后包装机的状态。

图1 自动包装机的结构



图 2 顺序按下按钮 3、2、3、0、1、2、0 后包装机的状态

一种特殊情况是,因为筐的容量是有限的,当筐已经满了,但仍然有某条轨道的按钮被按下时,系统应强制启动 \(0\) 号键,先从筐里抓出一件物品,再将对应轨道的物品推落。此外,如果轨道已经空了,再按对应的按钮不会发生任何事;同样的,如果筐是空的,按 \(0\) 号按钮也不会发生任何事。

现给定一系列按钮操作,请你依次列出流水线上的物品。

输入格式:

输入第一行给出 3 个正整数 \(N\)\(≤100\) )、\(M\)\(≤1000\) )和 \(S_{max}\)\(≤100\) ),分别为轨道的条数(于是轨道从 \(1\)\(N\) 编号)、每条轨道初始放置的物品数量、以及筐的最大容量。随后 \(N\) 行,每行给出 \(M\) 个英文大写字母,表示每条轨道的初始物品摆放。

最后一行给出一系列数字,顺序对应被按下的按钮编号,直到 \(−1\) 标志输入结束,这个数字不要处理。数字间以空格分隔。题目保证至少会取出一件物品放在流水线上。

输出格式:

在一行中顺序输出流水线上的物品,不得有任何空格。

输入样例:

3 4 4
GPLT
PATA
OMSA
3 2 3 0 1 2 0 2 2 0 -1

输出样例:

MATA


解题思路

模拟题。可以用来模拟筐,用队列来模拟多个轨道,基本上按照题意模拟就可以。

/*   一切都是命运石之门的选择  El Psy Kongroo  */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, int> psi;
//typedef __int128 int128;
#define PI acos(-1.0)
#define x first
#define y second
//int dx[4] = {1, -1, 0, 0};
//int dy[4] = {0, 0, 1, -1};
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;


const int N = 110;
int n, m, k;
queue<char> q[N];
stack<char> st;
string res = "";

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n >> m >> k;
    for(int i = 1; i <= n; i ++ ){
        string s; cin >> s;
        for(auto &ch : s) q[i].push(ch);
    }

    int op;
    while(cin >> op && ~op){
        if(op == 0){
            if(st.empty()) continue;     //筐为空
            res += st.top();             //取出顶部放入答案序列
            st.pop();
        }else{
            if(q[op].empty()) continue;  //该轨道为空
            while((int)st.size() >= k){  //筐已满
                res += st.top();
                st.pop();
            }
            st.push(q[op].front());      //取出轨道第一个放入筐中
            q[op].pop();
        }
    }

    cout << res << endl;

    return 0;
}