跳到正文

剪贴板

剪贴板 xw5pop2r

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <algorithm>
#include <cassert>
#include <deque>
#include <iostream>
#include <vector>

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

int main() {
    size_t n, m;
    std::cin >> n >> m;
    std::vector<size_t> v(n + m);
    for (size_t i = 0; i < n + m; i++) {
        std::cin >> v[i];
        v[i] -= n + m + 1;
        if (v[i] < n) {
            v[i] = n - 1 - v[i];
        }
        else {
            v[i] = n + (m - 1 - (v[i] - n));
        }
    }
    std::vector<std::vector<size_t>> rotate;
    std::vector<bool> vis(n + m);
    std::vector<size_t> pos(n + m);
    for (size_t i = 0; i < m + n; i++) {
        if (vis[i]) {
            continue;
        }
        rotate.emplace_back();
        size_t cur = i;
        while (v[cur] != i) {
            rotate.back().push_back(cur);
            vis[cur] = true;
            pos[cur] = rotate.size() - 1;
            cur = v[cur];
        }
        rotate.back().push_back(cur);
        pos[cur] = rotate.size() - 1;
        vis[cur] = true;
    }
    std::vector<std::vector<bool>> ans(n, std::vector<bool>(m));
    for (size_t i = 0; i < n; i++) {
        size_t cur_ind = pos[i];
        auto mid = std::find(rotate[cur_ind].begin(), rotate[cur_ind].end(), i);
        std::rotate(rotate[cur_ind].begin(), mid, rotate[cur_ind].end());
        size_t last = 0, pos = 1;
        while (pos < rotate[cur_ind].size() && rotate[cur_ind][pos] >= n &&
               rotate[cur_ind][pos] > last) {
            ans[i][rotate[cur_ind][pos] - n] = true;
            last = rotate[cur_ind][pos];
            pos++;
        }
        while (!rotate[cur_ind].empty() && rotate[cur_ind].size() > pos &&
               rotate[cur_ind].back() > last && rotate[cur_ind].back() >= n) {
            ans[i][rotate[cur_ind].back() - n] = true;
            rotate[cur_ind].pop_back();
        }
        if (pos != rotate[cur_ind].size()) {
            pos--;
            rotate[cur_ind].erase(rotate[cur_ind].begin(),
                                  rotate[cur_ind].begin() + pos);
        }
    }
    for (size_t i = 0; i < n; i++) {
        for (size_t j = 0; j < m; j++) {
            std::cout << ans[i][j] << " ";
        }
        std::cout << "\n";
    }
    std::cout << std::flush;
    return 0;
}

/**
 * 需要注意实际上题解里的轮坏 (a ,b, c) 表示 a 最后的位置是 b,
 * 而代码里表示的是 a 最后换成了 b 的值。因此是反着的。
 */