結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー phspls
提出日時 2022-11-20 16:38:50
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 1,115 bytes
コンパイル時間 13,189 ms
コンパイル使用メモリ 378,844 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-09-21 15:56:54
合計ジャッジ時間 27,257 ms
ジャッジサーバーID
(参考情報)
judge5 / 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 resx = pairs[0].0;
    let bsum = b.iter().sum::<isize>();
    let mut bleft = pairs[0].1;
    let mut result = pairs.iter().skip(1).map(|&(l, r)| (l - resx).abs() * r).sum::<isize>();
    let mut summary = result;
    for i in 1..n {
        let (px, _) = pairs[i-1];
        let (x, bval) = pairs[i];
        summary += (x - px) * bleft;
        summary -= (x - px) * (bsum - bleft);
        if result > summary {
            result = summary;
            resx = x;
        }
        bleft += bval;
    }
    println!("{} {}", resx, result);
}
0