結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー phsplsphspls
提出日時 2022-11-27 22:10:19
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,121 bytes
コンパイル時間 11,271 ms
コンパイル使用メモリ 402,440 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-10-04 13:32:50
合計ジャッジ時間 24,811 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    let mut pairs = (0..n).map(|i| (a[i], b[i])).collect::<Vec<_>>();
    pairs.sort();
    let mut result = pairs.iter().map(|&(a, b)| b * (a - pairs[0].0)).sum::<isize>();
    let mut summary = result;
    let mut pos = pairs[0].0;
    let btotal = b.iter().sum::<isize>();
    let mut leftb = pairs[0].1;
    for i in 1..n {
        let (px, _) = pairs[i-1];
        let (x, b) = pairs[i];
        let diff = x - px;
        summary += diff * leftb;
        summary -= diff * (btotal - leftb);
        leftb += b;
        if summary < result {
            result = summary;
            pos = x;
        }
    }
    println!("{} {}", pos, result);
}
0