結果

問題 No.918 LISGRID
ユーザー Mister
提出日時 2020-08-18 09:34:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 2,938 bytes
コンパイル時間 1,156 ms
コンパイル使用メモリ 88,932 KB
最終ジャッジ日時 2025-01-13 02:53:54
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

template <class Cost = int>
struct TopologicalSort {
    Graph<Cost> graph;
    std::vector<bool> visited;
    std::vector<int> order, id;

    explicit TopologicalSort(const Graph<Cost>& graph)
        : graph(graph), visited(graph.size(), false), id(graph.size()) {
        for (int v = 0; v < (int)graph.size(); ++v) dfs(v);
        std::reverse(order.begin(), order.end());

        for (int i = 0; i < (int)graph.size(); ++i) id[order[i]] = i;
    }

    void dfs(int v) {
        if (visited[v]) return;
        visited[v] = true;
        for (const auto& e : graph[v]) dfs(e.dst);
        order.push_back(v);
    }
};

void solve() {
    int h, w;
    std::cin >> h >> w;

    Graph<> graph(h * w);
    auto enc = [&](int x, int y) { return x * w + y; };

    std::vector<int> ps(h);
    for (auto& p : ps) std::cin >> p;
    std::sort(ps.begin(), ps.end());

    for (int x = 0; x < h; ++x) {
        int p = ps[x];
        for (int y = 0; y + 1 < p; ++y) {
            graph.span(true, enc(x, y), enc(x, y + 1));
        }

        if (p < w) graph.span(true, enc(x, p), enc(x, 0));

        for (int y = p; y + 1 < w; ++y) {
            graph.span(true, enc(x, y + 1), enc(x, y));
        }
    }

    std::vector<int> qs(w);
    for (auto& q : qs) std::cin >> q;
    std::sort(qs.begin(), qs.end());

    for (int y = 0; y < w; ++y) {
        int q = qs[y];
        for (int x = 0; x + 1 < q; ++x) {
            graph.span(true, enc(x, y), enc(x + 1, y));
        }

        if (q < h) graph.span(true, enc(q, y), enc(0, y));

        for (int x = q; x + 1 < h; ++x) {
            graph.span(true, enc(x + 1, y), enc(x, y));
        }
    }

    TopologicalSort ts(graph);

    for (int x = 0; x < h; ++x) {
        for (int y = 0; y < w; ++y) {
            std::cout << ts.id[enc(x, y)] + 1 << " ";
        }
        std::cout << "\n";
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0