結果

問題 No.838 Noelちゃんと星々3
ユーザー phsplsphspls
提出日時 2023-01-19 20:30:31
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,657 bytes
コンパイル時間 5,405 ms
コンパイル使用メモリ 151,016 KB
実行使用メモリ 4,492 KB
最終ジャッジ日時 2023-09-04 11:52:58
合計ジャッジ時間 6,553 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1 ms
4,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 1 ms
4,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 WA -
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
 --> Main.rs:3:5
  |
2 | struct UnionFind {
  |        --------- field in this struct
3 |     n: usize,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

struct UnionFind {
    n: usize,
    parents: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        UnionFind {
            n: n,
            parents: (0..n).collect(),
        }
    }

    fn equiv(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }
    
    fn unite(&mut self, a: usize, b: usize) {
        if self.equiv(a, b) { return; }
        let (a, b) = (a.min(b), a.max(b));
        let x = self.parents[a];
        let y = self.parents[b];
        self.parents[y] = self.parents[x];
    }

    fn find(&mut self, a: usize) -> usize {
        if self.parents[a] == a { return a; }
        let p = self.find(self.parents[a]);
        self.parents[a] = p;
        p
    }
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut y = String::new();
    std::io::stdin().read_line(&mut y).ok();
    let mut y: Vec<isize> = y.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    y.sort();
    let mut uf = UnionFind::new(n+1);
    let mut result = 0isize;
    for i in 0..n-1 {
        if i == 0 || i == n - 2 {
            uf.unite(i, n);
            uf.unite(i+1, n);
            result += y[i+1] - y[i];
            continue;
        }
        if uf.find(i) == uf.find(n) {
            continue;
        }
        if y[i] - y[i-1] + y[i+2] - y[i+1] <= y[i+1] - y[i] {
            uf.unite(i, n);
            result += y[i] - y[i-1];
        } else {
            uf.unite(i, n);
            uf.unite(i+1, n);
            result += y[i+1] - y[i];
        }
    }
    println!("{}", result);
}
0