結果

問題 No.918 LISGRID
ユーザー MisterMister
提出日時 2020-08-18 09:34:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 2,938 bytes
コンパイル時間 950 ms
コンパイル使用メモリ 93,204 KB
実行使用メモリ 22,628 KB
最終ジャッジ日時 2024-04-20 04:22:10
合計ジャッジ時間 4,458 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 28 ms
14,464 KB
testcase_02 AC 11 ms
7,168 KB
testcase_03 AC 13 ms
8,704 KB
testcase_04 AC 11 ms
7,552 KB
testcase_05 AC 36 ms
17,012 KB
testcase_06 AC 36 ms
16,000 KB
testcase_07 AC 47 ms
21,688 KB
testcase_08 AC 29 ms
14,208 KB
testcase_09 AC 38 ms
17,260 KB
testcase_10 AC 36 ms
17,280 KB
testcase_11 AC 37 ms
17,248 KB
testcase_12 AC 42 ms
18,608 KB
testcase_13 AC 33 ms
15,672 KB
testcase_14 AC 44 ms
20,504 KB
testcase_15 AC 49 ms
22,628 KB
testcase_16 AC 19 ms
10,368 KB
testcase_17 AC 19 ms
10,880 KB
testcase_18 AC 18 ms
10,368 KB
testcase_19 AC 6 ms
6,940 KB
testcase_20 AC 26 ms
13,184 KB
testcase_21 AC 10 ms
6,940 KB
testcase_22 AC 6 ms
6,940 KB
testcase_23 AC 18 ms
9,472 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 1 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 1 ms
6,944 KB
testcase_30 AC 1 ms
6,940 KB
testcase_31 AC 1 ms
6,944 KB
testcase_32 AC 1 ms
6,940 KB
testcase_33 AC 1 ms
6,940 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 3 ms
6,940 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 26 ms
13,056 KB
testcase_38 AC 16 ms
9,728 KB
権限があれば一括ダウンロードができます

ソースコード

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