結果

問題 No.2639 Longest Increasing Walk
ユーザー nono00nono00
提出日時 2024-02-19 22:42:06
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,443 bytes
コンパイル時間 3,196 ms
コンパイル使用メモリ 254,136 KB
実行使用メモリ 19,008 KB
最終ジャッジ日時 2024-02-19 22:42:12
合計ジャッジ時間 5,476 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 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 AC 41 ms
11,200 KB
testcase_05 AC 68 ms
19,008 KB
testcase_06 AC 66 ms
18,772 KB
testcase_07 AC 87 ms
18,740 KB
testcase_08 AC 67 ms
18,828 KB
testcase_09 AC 92 ms
18,980 KB
testcase_10 AC 46 ms
11,264 KB
testcase_11 AC 41 ms
10,496 KB
testcase_12 AC 8 ms
6,676 KB
testcase_13 AC 48 ms
11,968 KB
testcase_14 AC 29 ms
8,320 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 27 ms
8,192 KB
testcase_18 AC 35 ms
9,216 KB
testcase_19 AC 10 ms
6,676 KB
testcase_20 AC 22 ms
6,912 KB
testcase_21 AC 42 ms
10,496 KB
testcase_22 AC 16 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 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 <bits/stdc++.h>

void solve() {
    int h, w;
    std::cin >> h >> w;
    auto encode = [&](int i, int j) {
        return i * w + j;
    };
    std::vector 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 graph(h * w, std::vector<int>());
    const int DI[] = {1, 0, -1, 0};
    const int DJ[] = {0, 1, 0, -1};
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int k = encode(i, j);
            for (int d = 0; d < 4; d++) {
                int ni = i + DI[d];
                int nj = j + DJ[d];
                if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
                if (a[i][j] >= a[ni][nj]) continue;
                int nk = encode(ni, nj);
                graph[k].push_back(nk);
            }
        }
    }
    std::vector<int> dp(h * w, -1);
    auto dfs = [&](auto self, int u) -> int {
        if (dp[u] != -1) return dp[u];
        dp[u] = 1;
        for (int v: graph[u]) {
            dp[u] = std::max(dp[u], self(self, v) + 1);
        }
        return dp[u];
    };
    int ans = 0;
    for (int i = 0; i < h * w; i++) {
        ans = std::max(ans, dfs(dfs, i));
    }
    std::cout << ans << '\n';
}

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    std::cout << std::fixed << std::setprecision(16);
    int t = 1;

    while (t--) solve();
}
0