結果

問題 No.2639 Longest Increasing Walk
ユーザー 寝癖寝癖
提出日時 2024-02-19 23:03:13
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,887 bytes
コンパイル時間 1,162 ms
コンパイル使用メモリ 121,428 KB
実行使用メモリ 13,480 KB
最終ジャッジ日時 2024-02-19 23:03:21
合計ジャッジ時間 4,953 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,480 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>

int main() {
    int H, W;
    std::cin >> H >> W;
    std::vector<std::vector<int>> A(H, std::vector<int>(W));
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            std::cin >> A[i][j];
        }
    }

    std::vector<std::set<int>> to(H*W);
    for (int x = 0; x < H*W; ++x) {
        int i = x / W, j = x % W;
        int di[] = {1, 0, -1, 0};
        int dj[] = {0, 1, 0, -1};
        for (int k = 0; k < 4; ++k) {
            int ni = i + di[k], nj = j + dj[k];
            if (0 <= ni && ni < H && 0 <= nj && nj < W && A[ni][nj] > A[i][j]) {
                to[x].insert(ni*W+nj);
            }
        }
    }

    std::vector<int> indegree(H*W, 0);
    for (int x = 0; x < H*W; ++x) {
        for (auto y : to[x]) {
            indegree[y]++;
        }
    }

    std::queue<int> q;
    std::vector<int> B;
    for (int x = 0; x < H*W; ++x) {
        if (indegree[x] == 0) {
            q.push(x);
            B.push_back(x);
        }
    }

    std::vector<int> order;
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        order.push_back(x);
        for (auto y : to[x]) {
            indegree[y]--;
            if (indegree[y] == 0) {
                q.push(y);
            }
        }
    }

    int ans = 0;
    for (auto x : B) {
        int i = std::find(order.begin(), order.end(), x) - order.begin();
        std::vector<int> dist(H*W, -1);
        dist[x] = 1;
        while (i < order.size()) {
            x = order[i++];
            for (auto y : to[x]) {
                if (dist[y] < dist[x] + 1) {
                    dist[y] = dist[x] + 1;
                }
            }
        }
        ans = std::max(ans, *std::max_element(dist.begin(), dist.end()));
    }

    std::cout << ans << std::endl;

    return 0;
}
0