結果

問題 No.1391 ±1 Abs Sum
ユーザー phsplsphspls
提出日時 2022-11-27 22:55:07
言語 Rust
(1.77.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,350 bytes
コンパイル時間 755 ms
コンパイル使用メモリ 172,776 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 03:58:22
合計ジャッジ時間 3,380 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 0 ms
6,944 KB
testcase_02 AC 0 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 0 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 14 ms
6,940 KB
testcase_12 AC 11 ms
6,940 KB
testcase_13 AC 13 ms
6,940 KB
testcase_14 AC 10 ms
6,944 KB
testcase_15 AC 10 ms
6,940 KB
testcase_16 AC 11 ms
6,940 KB
testcase_17 AC 11 ms
6,940 KB
testcase_18 AC 14 ms
6,940 KB
testcase_19 AC 9 ms
6,944 KB
testcase_20 AC 14 ms
6,940 KB
testcase_21 AC 8 ms
6,940 KB
testcase_22 AC 9 ms
6,940 KB
testcase_23 AC 10 ms
6,944 KB
testcase_24 AC 9 ms
6,944 KB
testcase_25 AC 10 ms
6,944 KB
testcase_26 AC 9 ms
6,944 KB
testcase_27 AC 7 ms
6,940 KB
testcase_28 AC 7 ms
6,944 KB
testcase_29 AC 9 ms
6,944 KB
testcase_30 AC 7 ms
6,940 KB
testcase_31 AC 15 ms
6,940 KB
testcase_32 AC 7 ms
6,940 KB
testcase_33 AC 9 ms
6,940 KB
testcase_34 AC 11 ms
6,940 KB
testcase_35 AC 1 ms
6,944 KB
testcase_36 AC 13 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut nk = String::new();
    std::io::stdin().read_line(&mut nk).ok();
    let nk: Vec<usize> = nk.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nk[0];
    let k = nk[1];
    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 k = (n-k).min(n-1);
    let mut summary = (0..n).map(|i| a[i] - a[0]).sum::<isize>();
    let mut rights = (0..n).rev().take(k).collect::<Vec<_>>();
    summary -= rights.iter().map(|&i| a[i] - a[0]).sum::<isize>() * 2;
    let mut lidx = 0usize;
    let mut result = summary;
    for i in 1..n {
        let diff = a[i] - a[i-1];
        summary += i as isize * diff;
        summary -= lidx as isize * 2 * diff;
        summary -= (n  - i) as isize * diff;
        summary += rights.len() as isize * 2 * diff;
        while lidx < n && !rights.is_empty() {
            let rval = rights[rights.len()-1];
            if a[i] - a[lidx] <= a[rval] - a[i] {
                break;
            } else {
                summary -= (a[i] - a[lidx]) * 2;
                summary += (a[rval] - a[i]) * 2;
                lidx += 1;
                rights.pop();
            }
        }
        result = result.min(summary);
    }
    println!("{}", result);
}
0