結果

問題 No.1950 片道きゃっちぼーる
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2022-05-20 23:36:56
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,495 bytes
コンパイル時間 4,260 ms
コンパイル使用メモリ 171,208 KB
実行使用メモリ 18,556 KB
最終ジャッジ日時 2023-10-20 14:33:05
合計ジャッジ時間 11,159 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 332 ms
18,484 KB
testcase_04 AC 333 ms
18,484 KB
testcase_05 AC 0 ms
4,348 KB
testcase_06 AC 329 ms
18,484 KB
testcase_07 AC 314 ms
15,912 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 327 ms
18,532 KB
testcase_12 AC 330 ms
18,540 KB
testcase_13 AC 334 ms
18,484 KB
testcase_14 AC 338 ms
18,484 KB
testcase_15 WA -
testcase_16 AC 331 ms
18,484 KB
testcase_17 WA -
testcase_18 AC 325 ms
18,480 KB
testcase_19 WA -
testcase_20 AC 323 ms
15,704 KB
testcase_21 AC 336 ms
18,484 KB
testcase_22 AC 333 ms
18,480 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);
                uni.merge(i, j);
                max[uni.root(i)] = max[i].max(max[j]);
            }
        }
        if let Some(&j) = mp.get(&(x+a)) {
            let i = uni.root(i);
            let j = uni.root(j);

            uni.merge(i, j);
            max[uni.root(i)] = max[i].max(max[j]);
        }
    }

    for i in 0..n {
        println!("{}", max[uni.root(i)] - x[i]);
    }
}

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