結果

問題 No.2639 Longest Increasing Walk
ユーザー InTheBloomInTheBloom
提出日時 2024-02-19 22:11:17
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,723 bytes
コンパイル時間 1,031 ms
コンパイル使用メモリ 100,644 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-02-19 22:11:21
合計ジャッジ時間 3,779 ms
ジャッジサーバーID
(参考情報)
judge16 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 4 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 121 ms
8,320 KB
testcase_05 AC 113 ms
6,676 KB
testcase_06 AC 137 ms
6,676 KB
testcase_07 AC 112 ms
6,676 KB
testcase_08 AC 112 ms
6,676 KB
testcase_09 AC 114 ms
6,676 KB
testcase_10 AC 70 ms
6,676 KB
testcase_11 AC 63 ms
6,676 KB
testcase_12 AC 12 ms
6,676 KB
testcase_13 AC 75 ms
6,676 KB
testcase_14 AC 44 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 42 ms
6,676 KB
testcase_18 AC 52 ms
6,676 KB
testcase_19 AC 16 ms
6,676 KB
testcase_20 AC 33 ms
6,676 KB
testcase_21 AC 63 ms
6,676 KB
testcase_22 AC 24 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 3 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

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

    // DAGになるやつ -> 最長経路問題はdpで求まる
    auto is_in = [&](int i, int j) {
        return 0 <= i && i < H && 0 <= j && j < W;
    };

    vector<vector<int>> indeg(H, vector<int>(W));

    const vector<pair<int, int>> dxy = {
        make_pair(0, 1),
        make_pair(1, 0),
        make_pair(0, -1),
        make_pair(-1, 0),
    };

    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {
        for (auto& d : dxy) {
            int y = i + d.first;
            int x = j + d.second;

            if (!is_in(y, x)) continue;
            if (A[i][j] < A[y][x]) indeg[y][x]++;
        }
    }

    vector<vector<int>> vis(H, vector<int>(W, -1));
    queue<pair<int, int>> Q;

    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {
        if (indeg[i][j] == 0) Q.push(make_pair(i, j));
    }

    while (!Q.empty()) {
        auto pos = Q.front(); Q.pop();
        int i = pos.first;
        int j = pos.second;
        if (vis[i][j] == -1) vis[i][j] = 0;

        for (auto& d : dxy) {
            int y = i + d.first;
            int x = j + d.second;
            if (!is_in(y, x)) continue;
            if (A[y][x] <= A[i][j]) continue;

            vis[y][x] = max(vis[y][x], vis[i][j] + 1);

            indeg[y][x]--;
            if (indeg[y][x] == 0) Q.push(make_pair(y, x));
        }
    }

    int ans = 0;
    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) ans = max(ans, vis[i][j] + 1);

    cout << ans << "\n";
}
0