Submission #16768460


Source Code Expand

use proconio::input;

fn main() {
    input! {
        h: usize,
        w: usize,
        a: [[i64; w]; h],
    };
    let mut e_i = vec![vec![]; w * h];
    let mut e_o = vec![vec![]; w * h];
    for r in 0..h {
        for c in 0..w {
            let v = a[r][c];
            let dir = vec![(-1, 0), (0, -1), (0, 1), (1, 0)];
            for (d_r, d_c) in dir.iter() {
                let (r_n, c_n) = (r as i64 + d_r, c as i64 + d_c);
                if (0..h as i64).contains(&r_n) && (0..w as i64).contains(&c_n) {
                    let (r_n, c_n) = (r_n as usize, c_n as usize);
                    let v_n = a[r_n][c_n];
                    if v_n < v {
                        e_i[r * w + c].push(r_n * w + c_n);
                    } else if v_n > v {
                        e_o[r * w + c].push(r_n * w + c_n);
                    }
                }
            }
        }
    }
    let mut b = vec![];
    for r in 0..h {
        for c in 0..w {
            b.push((a[r][c], r, c));
        }
    }
    b.sort_by_key(|(v, _, _)| -v);
    let mut done = vec![false; h * w];
    let mut d = vec![0; h * w];
    for &(_, br, bc) in b.iter() {
        if done[br * w + bc] {
            continue;
        }
        let mut q = std::collections::VecDeque::new();
        q.push_back(br * w + bc);
        while let Some(p) = q.pop_front() {
            done[p] = true;
            d[p] += 1;
            d[p] %= 1_000_000_007;
            for &p_i in e_i[p].iter() {
                d[p_i] += d[p];
                d[p_i] %= 1_000_000_007;
                for j in 0..e_o[p_i].len() {
                    if e_o[p_i][j] == p {
                        e_o[p_i].remove(j);
                        break;
                    }
                }
                if e_o[p_i].is_empty() {
                    q.push_back(p_i);
                }
            }
        }
    }
    let mut ans = 0;
    for &d_i in d.iter() {
        ans += d_i;
        ans %= 1_000_000_007;
    }
    println!("{}", ans);
}

Submission Info

Submission Time
Task D - 経路
User bouzuya
Language Rust (1.42.0)
Score 100
Code Size 2011 Byte
Status AC
Exec Time 690 ms
Memory 160328 KiB

Judge Result

Set Name sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 24
Set Name Test Cases
sample sample01.txt, sample02.txt
All 00.txt, 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, sample01.txt, sample02.txt, sample01.txt, sample02.txt
Case Name Status Exec Time Memory
00.txt AC 467 ms 158548 KiB
01.txt AC 319 ms 157496 KiB
02.txt AC 355 ms 141868 KiB
03.txt AC 1 ms 2068 KiB
04.txt AC 1 ms 2044 KiB
05.txt AC 2 ms 2084 KiB
06.txt AC 2 ms 2228 KiB
07.txt AC 1 ms 2308 KiB
08.txt AC 2 ms 2164 KiB
09.txt AC 2 ms 2052 KiB
10.txt AC 4 ms 2532 KiB
11.txt AC 682 ms 160224 KiB
12.txt AC 684 ms 160328 KiB
13.txt AC 684 ms 160284 KiB
14.txt AC 682 ms 160312 KiB
15.txt AC 565 ms 159800 KiB
16.txt AC 690 ms 160312 KiB
17.txt AC 689 ms 160208 KiB
18.txt AC 304 ms 124904 KiB
19.txt AC 127 ms 86988 KiB
sample01.txt AC 1 ms 1908 KiB
sample02.txt AC 1 ms 2040 KiB