結果

問題 No.1950 片道きゃっちぼーる
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2022-05-20 23:58:38
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,740 bytes
コンパイル時間 1,728 ms
コンパイル使用メモリ 181,556 KB
実行使用メモリ 18,560 KB
最終ジャッジ日時 2023-10-20 14:54:24
合計ジャッジ時間 10,272 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 320 ms
18,460 KB
testcase_04 AC 301 ms
18,460 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 310 ms
18,460 KB
testcase_07 AC 304 ms
15,888 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 302 ms
18,508 KB
testcase_12 AC 307 ms
18,516 KB
testcase_13 AC 312 ms
18,460 KB
testcase_14 AC 313 ms
18,460 KB
testcase_15 WA -
testcase_16 AC 316 ms
18,460 KB
testcase_17 WA -
testcase_18 AC 309 ms
18,456 KB
testcase_19 WA -
testcase_20 AC 296 ms
15,680 KB
testcase_21 AC 313 ms
18,460 KB
testcase_22 AC 308 ms
18,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::BTreeMap;

fn main() {
    let n : usize = input();
    let x : Vec<usize> = input_vec();
    let a : Vec<usize> = input_vec();

    let mut max = vec![0; n];
    let mut mp = BTreeMap::new();
    let mut uni = UnionFind::new(n);
    for i in 0..n {
        max[i] = x[i] + a[i];
        mp.insert(x[i], i);
    }

    for i in 0..n {
        let x = x[i];
        let a = a[i];
        if a <= x {
            if let Some(&j) = mp.get(&(x-a)) {
                let i = uni.root(i);
                let j = uni.root(j);
                if uni.merge(i, j) {
                    max[i] = max[i].max(max[j]);
                    max[j] = max[i].max(max[j]);
                    max[i] = max[i].max(max[j]);
                }
            }
        }
        if let Some(&j) = mp.get(&(x+a)) {
            let i = uni.root(i);
            let j = uni.root(j);
            if uni.merge(i, j) {
                max[i] = max[i].max(max[j]);
                max[j] = max[i].max(max[j]);
                max[i] = max[i].max(max[j]);
            }        }
    }

    for i in 0..n {
        println!("{}", max[uni.root(i)] - x[i]);
    }
}
/*
4
0 10 20 30
30 10 10 0
 */

struct UnionFind { per : Vec<Option<usize>>, size : Vec<usize> } #[allow(dead_code)] impl UnionFind { fn new (n : usize) -> Self { Self { per : vec![None; n+1], size: vec![1; n+1]} } fn root (&mut self, x : usize) -> usize { if let Some(y) = self.per[x] { let res = self.root(y); self.per[x] = Some(res); res } else { x } } fn merge(&mut self, mut x : usize, mut y : usize) -> bool { x = self.root(x); y = self.root(y); if x == y { return false; } if self.size[x] < self.size[y] { std::mem::swap(&mut x, &mut y); } self.per[y] = Some(x); self.size[x] += self.size[y]; true } fn same(&mut self, x : usize, y : usize) -> bool { self.root(x) == self.root(y) } fn size(&mut self, x : usize) -> usize { let x = self.root(x); self.size[x] } }
#[allow(dead_code)] fn input<T: std::str::FromStr>() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } #[allow(dead_code)] fn input_t<T: std::str::FromStr, U: std::str::FromStr>() -> (T, U) { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let s = s.trim().split_whitespace().collect::<Vec<&str>>(); (s[0].parse().ok().unwrap(), s[1].parse().ok().unwrap()) } #[allow(dead_code)] fn input_t3<T1: std::str::FromStr, T2: std::str::FromStr, T3: std::str::FromStr>() -> (T1, T2, T3) { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let s = s.trim().split_whitespace().collect::<Vec<&str>>(); (s[0].parse().ok().unwrap(), s[1].parse().ok().unwrap(), s[2].parse().ok().unwrap()) } #[allow(dead_code)] fn input_t4<T1: std::str::FromStr, T2: std::str::FromStr, T3: std::str::FromStr, T4: std::str::FromStr>() -> (T1, T2, T3, T4) { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let s = s.trim().split_whitespace().collect::<Vec<&str>>(); (s[0].parse().ok().unwrap(), s[1].parse().ok().unwrap(), s[2].parse().ok().unwrap(), s[3].parse().ok().unwrap()) } #[allow(dead_code)] fn input_t5<T1: std::str::FromStr, T2: std::str::FromStr, T3: std::str::FromStr, T4: std::str::FromStr, T5: std::str::FromStr>() -> (T1, T2, T3, T4, T5) { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let s = s.trim().split_whitespace().collect::<Vec<&str>>(); (s[0].parse().ok().unwrap(), s[1].parse().ok().unwrap(), s[2].parse().ok().unwrap(), s[3].parse().ok().unwrap(), s[4].parse().ok().unwrap()) } #[allow(dead_code)] fn input_vec<T: std::str::FromStr>() -> Vec<T> { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().split_whitespace().map(|s| s.parse().ok().unwrap()).collect() }
0