結果

問題 No.1391 ±1 Abs Sum
ユーザー phsplsphspls
提出日時 2022-11-21 01:55:30
言語 Rust
(1.77.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,314 bytes
コンパイル時間 1,919 ms
コンパイル使用メモリ 177,768 KB
実行使用メモリ 7,516 KB
最終ジャッジ日時 2023-10-21 20:10:03
合計ジャッジ時間 3,955 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 0 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 17 ms
6,816 KB
testcase_12 AC 13 ms
5,412 KB
testcase_13 AC 15 ms
6,424 KB
testcase_14 AC 12 ms
5,724 KB
testcase_15 AC 12 ms
5,800 KB
testcase_16 AC 16 ms
6,560 KB
testcase_17 AC 14 ms
6,188 KB
testcase_18 AC 18 ms
7,516 KB
testcase_19 AC 11 ms
4,712 KB
testcase_20 AC 17 ms
6,776 KB
testcase_21 AC 10 ms
4,348 KB
testcase_22 AC 10 ms
4,348 KB
testcase_23 AC 11 ms
4,716 KB
testcase_24 AC 11 ms
4,616 KB
testcase_25 AC 11 ms
4,636 KB
testcase_26 AC 11 ms
4,700 KB
testcase_27 AC 8 ms
4,348 KB
testcase_28 AC 9 ms
4,348 KB
testcase_29 AC 11 ms
4,588 KB
testcase_30 AC 9 ms
4,348 KB
testcase_31 AC 18 ms
7,248 KB
testcase_32 AC 10 ms
5,484 KB
testcase_33 AC 11 ms
5,876 KB
testcase_34 AC 13 ms
5,304 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 16 ms
5,892 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 lefts = vec![];
    let mut rights = (0..n).rev().take(k).collect::<Vec<_>>();
    let summary = (0..n).map(|i| a[i] - a[0]).sum::<isize>();
    let mut result = summary - 2 * rights.iter().map(|&i| a[i] - a[0]).sum::<isize>();
    let mut current = result;
    let mut lidx = 0usize;
    for i in 1..n {
        let diff = a[i] - a[i-1];
        current += i as isize * diff;
        current -= (n - i) as isize * diff;
        current -= 2 * lefts.len() as isize * diff;
        current += 2 * rights.len() as isize * diff;
        while rights.len() > 0 && a[i] - a[lidx] > a[rights[rights.len()-1]] - a[i] {
            current += 2 * (a[rights[rights.len()-1]] - a[i]);
            current -= 2 * (a[i] - a[lidx]);
            rights.pop();
            lefts.push(lidx);
            lidx += 1;
        }
        result = result.min(current);
    }
    println!("{}", result);
}
0