結果

問題 No.837 Noelちゃんと星々2
ユーザー phspls
提出日時 2020-07-12 16:30:07
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 1,675 bytes
コンパイル時間 14,022 ms
コンパイル使用メモリ 378,404 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-15 13:51:03
合計ジャッジ時間 15,320 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

fn calc_val(y: &Vec<isize>, start_idx: usize, count: usize) -> usize {
    let base_val: usize = (2*start_idx + count - 1) / 2;
    let base_val: isize = y[base_val];
    y.iter().skip(start_idx).take(count)
        .map(|i| (i - base_val).abs() as usize)
        .sum()
}

fn calc(y: &Vec<isize>, split_idx: usize) -> usize {
    calc_val(y, 0, split_idx) + calc_val(y, split_idx, y.len() - split_idx)
}

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();
    if y[0] == y[n-1] {
        println!("{}", 1);
        return;
    }
    let mut left: usize = 1;
    let mut right: usize = n;
    while right - left > 3 {
        let mleft  = (2*left + right) / 3;
        let mright = (left + 2*right) / 3;
        let ll = calc(&y, left);
        let ml = calc(&y, mleft);
        let mr = calc(&y, mright);
        let rr = calc(&y, right);
        let minval = vec![ll, ml, mr, rr].into_iter().min().unwrap();
        if ll == minval {
            right = mleft;
        } else if rr == minval {
            left = mright;
        } else if ml == minval {
            right = mright;
        } else {
            left = mleft;
        }
    }
    let mleft  = (2*left + right) / 3;
    let mright = (left + 2*right) / 3;
    let ll = calc(&y, left);
    let ml = calc(&y, mleft);
    let mr = calc(&y, mright);
    let rr = calc(&y, right);
    println!("{}", vec![ll, ml, mr, rr].iter().min().unwrap());
}
0