結果

問題 No.1391 ±1 Abs Sum
ユーザー lorent_kyoprolorent_kyopro
提出日時 2021-02-13 00:01:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 840 bytes
コンパイル時間 2,023 ms
コンパイル使用メモリ 202,220 KB
実行使用メモリ 6,808 KB
最終ジャッジ日時 2023-09-27 07:47:36
合計ジャッジ時間 5,732 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 AC 1 ms
4,380 KB
testcase_10 WA -
testcase_11 AC 83 ms
6,760 KB
testcase_12 AC 64 ms
6,220 KB
testcase_13 WA -
testcase_14 AC 47 ms
4,928 KB
testcase_15 WA -
testcase_16 AC 63 ms
6,216 KB
testcase_17 WA -
testcase_18 AC 74 ms
6,496 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 42 ms
4,644 KB
testcase_33 AC 48 ms
4,884 KB
testcase_34 AC 78 ms
6,572 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 89 ms
6,696 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[r] - a[l] > a[r + 1] - a[l + 1]) 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