結果

問題 No.198 キャンディー・ボックス2
ユーザー tonyu0
提出日時 2020-04-26 15:59:34
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 1,015 bytes
コンパイル時間 13,389 ms
コンパイル使用メモリ 379,508 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-17 10:19:48
合計ジャッジ時間 14,633 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

fn f(a: &[i64], b: i64, x: i64) -> bool {
    let mut cnt1 = 0;
    let mut cnt2 = 0;
    for i in 0..a.len() {
        if a[i] > x {
            cnt1 += a[i] - x;
        } else {
            cnt2 += x - a[i];
        }
    }
    if cnt1 + b >= cnt2 {
        true
    } else {
        false
    }
}

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();

    let b: i64 = itr.next().unwrap().parse().unwrap();
    let n: usize = itr.next().unwrap().parse().unwrap();
    let c: Vec<i64> = (0..n)
        .map(|_| itr.next().unwrap().parse().unwrap())
        .collect();

    let mut l = 0;
    let mut r = *c.iter().max().unwrap() + 1;
    for _ in 0..1000 {
        let mid = (l + r) >> 1;
        if f(&c, b, mid) {
            l = mid;
        } else {
            r = mid;
        }
    }

    let mut ans = 0;
    for i in 0..n {
        ans += (c[i] - l).abs();
    }
    println!("{}", ans);
}
0