結果

問題 No.2639 Longest Increasing Walk
ユーザー ponjuiceponjuice
提出日時 2024-02-13 14:43:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 1,623 bytes
コンパイル時間 4,269 ms
コンパイル使用メモリ 211,940 KB
実行使用メモリ 20,008 KB
最終ジャッジ日時 2024-02-19 20:50:13
合計ジャッジ時間 4,750 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 112 ms
13,224 KB
testcase_05 AC 129 ms
20,008 KB
testcase_06 AC 124 ms
19,644 KB
testcase_07 AC 137 ms
19,612 KB
testcase_08 AC 133 ms
19,828 KB
testcase_09 AC 145 ms
19,852 KB
testcase_10 AC 78 ms
12,032 KB
testcase_11 AC 70 ms
11,136 KB
testcase_12 AC 13 ms
6,676 KB
testcase_13 AC 87 ms
12,584 KB
testcase_14 AC 50 ms
8,704 KB
testcase_15 AC 1 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 49 ms
8,576 KB
testcase_18 AC 57 ms
9,728 KB
testcase_19 AC 18 ms
6,676 KB
testcase_20 AC 36 ms
7,168 KB
testcase_21 AC 72 ms
11,008 KB
testcase_22 AC 29 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 3 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<bits/stdc++.h>
using namespace std;

int main(){
    int h, w;
    cin >> h >> w;
    const auto id = [&](int x, int y)->int {
        return x * w + y;
    };
    vector a(h, vector<int>(w));
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> a[i][j];
        }
    }

    vector graph(h * w, vector<int>(0));
    vector in(h * w, 0);

    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(i != 0){
                if(a[i][j] < a[i - 1][j]){
                    graph[id(i, j)].emplace_back(id(i - 1, j));
                    in[id(i - 1, j)]++;
                }
                if(a[i][j] > a[i - 1][j]){
                    graph[id(i - 1, j)].emplace_back(id(i, j));
                    in[id(i, j)]++;
                }
            }
            if(j != 0){
                if(a[i][j] < a[i][j - 1]){
                    graph[id(i, j)].emplace_back(id(i, j - 1));
                    in[id(i, j - 1)]++;
                }
                if(a[i][j] > a[i][j - 1]){
                    graph[id(i, j - 1)].emplace_back(id(i, j));
                    in[id(i, j)]++;
                }
            }
        }
    }

    queue<int> que;
    vector depth(h * w, 1);
    for(int i = 0; i < h * w; i++){
        if(in[i] == 0) que.push(i);
    }

    while(que.size()){
        int p = que.front();
        que.pop();
        for(int to : graph[p]){
            in[to]--;
            depth[to] = max(depth[to], depth[p] + 1);
            if(in[to] == 0) que.push(to);
        }
    }

    cout << *max_element(depth.begin(), depth.end()) << endl;
}
0