ylh_的博客

天梯赛L2题解(037-040)

L2-037 包装机

挺好的小模拟

#include <bits/stdc++.h>
using namespace std;
#define int long long
using PII = pair<int, int>;

void ylh_() {
    int n, m, s;
    cin >> n >> m >> s;
    stack<char> st;
    vector<queue<char>> q(n + 1); // 其实用队列,栈,数组啥的都行,感觉队列好模拟一点
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            char c;
            cin >> c;
            q[i].push(c);
        }
    }
    vector<char> ans;
    auto op0 = [&]() -> void {
        if (st.empty()) {
            return;
        }
        ans.push_back(st.top());
        st.pop();
    };
    auto op = [&](int x) -> void {
        if (q[x].size() == 0) {
            return;
        }
        if (st.size() == s) {
            op0();
        }
        st.push(q[x].front());
        q[x].pop();
    };
    int x;
    while (cin >> x) {
        if (x == -1) {
            for (int i = 0; i < ans.size(); ++i) {
                cout << ans[i];
            }
            return;
        } else {
            (x == 0) ? op0() : op(x);
        }
    }
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) {
        ylh_();
    }
}

L2-038 病毒溯源

bfs或者dfs都可以,我这里用的dfs
一个是要知道怎么记录路径(dfs回溯,基础知识)
另外就是要知道怎么获取字典序最小(给每个点的边排序就可以控制遍历顺序)

#include <bits/stdc++.h>
using namespace std;
#define int long long

using PII = pair<int, int>;
const int INF = 2e18;

void ylh_() {
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n; ++i) {
        int k;
        cin >> k;
        for (int j = 1; j <= k; ++j) {
            int x;
            cin >> x;
            g[i].push_back(x);
        }
        sort(g[i].begin(), g[i].end());
    }
    vector<int> res, ans;
    int Max = 0;
    auto dfs = [&](auto&& dfs, int u) -> void {
        if (res.size() > Max) {
            Max = res.size();
            ans = res;
        }
        for (auto v : g[u]) {
            res.push_back(v);
            dfs(dfs, v);
            res.pop_back();
        }
    };
    for (int i = 0; i < n; ++i) {
        res.clear();
        res.push_back(i);
        dfs(dfs, i);
    }
    cout << ans.size() << '\n';
    for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i];
        if (i < ans.size() - 1) {
            cout << ' ';
        }
    }
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) {
        ylh_();
    }
}

L2-039 清点代码库

STL瞎写一下就行,之所以放负的cnt是因为set本来是顺序嘛,负数倒一倒就行了

#include <bits/stdc++.h>
using namespace std;
#define int long long

using PII = pair<int, int>;
const int INF = 2e18;

void ylh_() {
    int n, m;
    cin >> n >> m;
    map<vector<int>, int> mp;
    for (int i = 1; i <= n; ++i) {
        vector<int> a(m);
        for (int j = 0; j < m; ++j) {
            cin >> a[j];
        }
        mp[a]++;
    }
    set<pair<int, vector<int>>> ans;
    for (auto [vec, cnt] : mp) {
        ans.insert({ -cnt, vec });
    }
    cout << ans.size() << '\n';
    for (auto [cnt, vec] : ans) {
        cout << -cnt << ' ';
        for (int i = 0; i < m; ++i) {
            cout << vec[i] << " \n"[i == m - 1];
        }
    }
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) {
        ylh_();
    }
}

L2-040 哲哲打游戏

依照题意模拟即可

#include <bits/stdc++.h>
using namespace std;
#define int long long

using PII = pair<int, int>;
using AIT = array<int, 3>;

void ylh_() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n + 1);
    for (int i = 1; i <= n; ++i) {
        int k;
        cin >> k;
        a[i].resize(k + 1);
        a[i][0] = k;
        for (int j = 1; j <= k; ++j) {
            cin >> a[i][j];
        }
    }
    vector<int> cd(101); // 存档
    int pos = 1;
    for (int i = 1; i <= m; ++i) {
        int op, x;
        cin >> op >> x;
        if (op == 1) {
            cd[x] = pos;
            cout << pos << '\n';
        } else if (op == 2) {
            pos = cd[x];
        } else {
            pos = a[pos][x];
        }
    }
    cout << pos;
}

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) {
        ylh_();
    }
}
posted @ 2026-04-02 15:18  ylh-  阅读(20)  评论(0)    收藏  举报