結果

問題 No.2639 Longest Increasing Walk
ユーザー nautnaut
提出日時 2024-02-19 22:33:04
言語 Rust
(1.77.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 2,567 bytes
コンパイル時間 1,047 ms
コンパイル使用メモリ 187,720 KB
実行使用メモリ 11,628 KB
最終ジャッジ日時 2024-02-19 22:33:08
合計ジャッジ時間 3,174 ms
ジャッジサーバーID
(参考情報)
judge16 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 64 ms
11,628 KB
testcase_05 AC 43 ms
11,628 KB
testcase_06 AC 44 ms
11,472 KB
testcase_07 AC 63 ms
11,460 KB
testcase_08 AC 43 ms
11,612 KB
testcase_09 AC 62 ms
11,620 KB
testcase_10 AC 41 ms
9,368 KB
testcase_11 AC 37 ms
6,784 KB
testcase_12 AC 5 ms
6,676 KB
testcase_13 AC 45 ms
7,808 KB
testcase_14 AC 24 ms
6,676 KB
testcase_15 AC 1 ms
6,676 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 AC 27 ms
6,676 KB
testcase_18 AC 29 ms
6,676 KB
testcase_19 AC 8 ms
6,676 KB
testcase_20 AC 16 ms
6,676 KB
testcase_21 AC 36 ms
6,784 KB
testcase_22 AC 12 ms
6,676 KB
testcase_23 AC 1 ms
6,676 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 1 ms
6,676 KB
testcase_27 AC 1 ms
6,676 KB
testcase_28 AC 1 ms
6,676 KB
testcase_29 AC 1 ms
6,676 KB
testcase_30 AC 1 ms
6,676 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let H = input!(usize);
    let W = input!(usize);
    let A = (0..H).map(|_| input!(usize, W)).collect::<Vec<_>>();

    let mut hq = std::collections::BinaryHeap::new();

    for i in 0..H {
        for j in 0..W {
            hq.push((A[i][j], i, j));
        }
    }

    let mut dp = vec![vec![0_usize; W]; H];

    while let Some((_, y, x)) = hq.pop() {
        dp[y][x] = 1;

        if y > 0 && A[y][x] < A[y - 1][x] {
            dp[y][x] = std::cmp::max(dp[y][x], dp[y - 1][x] + 1);
        }

        if x > 0 && A[y][x] < A[y][x - 1] {
            dp[y][x] = std::cmp::max(dp[y][x], dp[y][x - 1] + 1);
        }

        if y + 1 < H && A[y][x] < A[y + 1][x] {
            dp[y][x] = std::cmp::max(dp[y][x], dp[y + 1][x] + 1);
        }

        if x + 1 < W && A[y][x] < A[y][x + 1] {
            dp[y][x] = std::cmp::max(dp[y][x], dp[y][x + 1] + 1);
        }
    }

    let ans = (0..H).map(|i| *dp[i].iter().max().unwrap()).max().unwrap();
    writeln!(out, "{}", ans);

    // for i in 0..H {
    //     eprintln!(
    //         "{}",
    //         dp[i]
    //             .iter()
    //             .map(|x| x.to_string())
    //             .collect::<Vec<_>>()
    //             .join(" ")
    //     );
    // }
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0