結果

問題 No.1950 片道きゃっちぼーる
ユーザー zeronosu77108
提出日時 2022-05-20 23:58:38
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 3,740 bytes
コンパイル時間 13,398 ms
コンパイル使用メモリ 387,320 KB
実行使用メモリ 18,692 KB
最終ジャッジ日時 2024-09-20 10:23:46
合計ジャッジ時間 21,407 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17 WA * 6
権限があれば一括ダウンロードができます

ソースコード

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