結果

問題 No.1391 ±1 Abs Sum
ユーザー lorent_kyoprolorent_kyopro
提出日時 2021-02-13 00:06:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 836 bytes
コンパイル時間 2,286 ms
コンパイル使用メモリ 203,616 KB
実行使用メモリ 6,812 KB
最終ジャッジ日時 2023-09-27 07:55:11
合計ジャッジ時間 5,212 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 78 ms
6,696 KB
testcase_12 AC 57 ms
6,168 KB
testcase_13 AC 72 ms
6,424 KB
testcase_14 AC 44 ms
4,884 KB
testcase_15 AC 47 ms
5,012 KB
testcase_16 AC 60 ms
6,416 KB
testcase_17 AC 52 ms
6,072 KB
testcase_18 AC 69 ms
6,456 KB
testcase_19 AC 50 ms
6,224 KB
testcase_20 AC 74 ms
6,560 KB
testcase_21 AC 48 ms
6,364 KB
testcase_22 AC 47 ms
6,272 KB
testcase_23 AC 55 ms
6,632 KB
testcase_24 AC 53 ms
6,376 KB
testcase_25 AC 52 ms
6,536 KB
testcase_26 AC 58 ms
6,472 KB
testcase_27 AC 36 ms
4,892 KB
testcase_28 AC 40 ms
4,992 KB
testcase_29 AC 52 ms
6,356 KB
testcase_30 AC 44 ms
6,152 KB
testcase_31 AC 77 ms
6,692 KB
testcase_32 AC 37 ms
4,688 KB
testcase_33 AC 44 ms
4,828 KB
testcase_34 AC 70 ms
6,812 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 84 ms
6,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    size_t n, k;
    cin >> n >> k;
    vector<long long> a(n);
    for (auto& ai : a) cin >> ai;
    if (k == 0) {
        auto calc = [a](long long x) {
            long long res = 0;
            for (auto ai : a) res -= abs(x - ai);
            return res;
        };
        cout << min(calc(a.front()), calc(a.back())) << '\n';
        return 0;
    }
    vector<long long> s(1);
    partial_sum(a.begin(), a.end(), back_inserter(s));
    long long ans = LLONG_MAX;
    size_t l = 0, r = k - 1;
    for (size_t i = 0; i < n; ++i) {
        while (r + 1 < n && a[i] - a[l] > a[r + 1] - a[i]) l++, r++;
        long long now = (n - 2 * l - 2 * r + 2 * i) * a[i] + 2 * (s[l] + s[r + 1] - s[i + 1]) - s[0] - s[n];
        ans = min(ans, now);
    }
    cout << ans << '\n';
}
0