結果

問題 No.1950 片道きゃっちぼーる
ユーザー atcoder8atcoder8
提出日時 2022-09-10 17:54:59
言語 Rust
(1.77.0)
結果
AC  
実行時間 474 ms / 3,000 ms
コード長 1,979 bytes
コンパイル時間 26,912 ms
コンパイル使用メモリ 379,476 KB
実行使用メモリ 22,560 KB
最終ジャッジ日時 2024-05-05 04:48:18
合計ジャッジ時間 26,146 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 454 ms
22,168 KB
testcase_04 AC 375 ms
22,300 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 342 ms
22,300 KB
testcase_07 AC 339 ms
13,200 KB
testcase_08 AC 378 ms
13,204 KB
testcase_09 AC 344 ms
22,124 KB
testcase_10 AC 329 ms
16,012 KB
testcase_11 AC 307 ms
17,008 KB
testcase_12 AC 335 ms
16,508 KB
testcase_13 AC 341 ms
19,740 KB
testcase_14 AC 348 ms
22,304 KB
testcase_15 AC 457 ms
22,284 KB
testcase_16 AC 332 ms
22,296 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 331 ms
17,436 KB
testcase_19 AC 474 ms
22,276 KB
testcase_20 AC 305 ms
12,812 KB
testcase_21 AC 350 ms
22,560 KB
testcase_22 AC 337 ms
22,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::{cmp::Reverse, collections::VecDeque};

fn main() {
    let n = {
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        line.trim().parse::<usize>().unwrap()
    };
    let xx = {
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|x| x.parse::<usize>().unwrap())
            .collect::<Vec<_>>()
    };
    let aa = {
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|x| x.parse::<usize>().unwrap())
            .collect::<Vec<_>>()
    };

    let mut graph = vec![vec![]; n];

    for curr_idx in 0..n {
        if xx[curr_idx] >= aa[curr_idx] {
            if let Ok(next_idx) = xx.binary_search(&(xx[curr_idx] - aa[curr_idx])) {
                graph[next_idx].push(curr_idx);
            }
        }

        if let Ok(next_idx) = xx.binary_search(&(xx[curr_idx] + aa[curr_idx])) {
            graph[next_idx].push(curr_idx);
        }
    }

    let mut sorted_indexes: Vec<usize> = (0..n).collect();
    sorted_indexes.sort_unstable_by_key(|&i| Reverse(xx[i] + aa[i]));
    let mut falling_positions = vec![0; n];

    for &leader_idx in sorted_indexes.iter() {
        if falling_positions[leader_idx] != 0 {
            continue;
        }

        let falling_position = xx[leader_idx] + aa[leader_idx];
        falling_positions[leader_idx] = falling_position;
        let mut que = VecDeque::from(vec![leader_idx]);

        while let Some(curr) = que.pop_front() {
            for &next in graph[curr].iter() {
                if falling_positions[next] == 0 {
                    falling_positions[next] = falling_position;
                    que.push_back(next);
                }
            }
        }
    }

    for (&dist, &x) in falling_positions.iter().zip(xx.iter()) {
        println!("{}", dist - x);
    }
}
0