結果

問題 No.1391 ±1 Abs Sum
ユーザー sten_sansten_san
提出日時 2021-02-13 11:20:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 1,660 bytes
コンパイル時間 1,975 ms
コンパイル使用メモリ 201,684 KB
実行使用メモリ 4,732 KB
最終ジャッジ日時 2023-09-27 19:41:18
合計ジャッジ時間 5,974 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 72 ms
4,612 KB
testcase_12 AC 53 ms
4,376 KB
testcase_13 AC 65 ms
4,500 KB
testcase_14 AC 43 ms
4,376 KB
testcase_15 AC 45 ms
4,376 KB
testcase_16 AC 58 ms
4,380 KB
testcase_17 AC 49 ms
4,376 KB
testcase_18 AC 65 ms
4,480 KB
testcase_19 AC 46 ms
4,380 KB
testcase_20 AC 68 ms
4,544 KB
testcase_21 AC 45 ms
4,440 KB
testcase_22 AC 45 ms
4,436 KB
testcase_23 AC 57 ms
4,432 KB
testcase_24 AC 49 ms
4,376 KB
testcase_25 AC 49 ms
4,380 KB
testcase_26 AC 50 ms
4,628 KB
testcase_27 AC 36 ms
4,380 KB
testcase_28 AC 36 ms
4,380 KB
testcase_29 AC 48 ms
4,376 KB
testcase_30 AC 42 ms
4,380 KB
testcase_31 AC 78 ms
4,576 KB
testcase_32 AC 35 ms
4,380 KB
testcase_33 AC 44 ms
4,376 KB
testcase_34 AC 66 ms
4,732 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 76 ms
4,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return std::vector(arg, init);
    else return std::vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

int main() {
    int64_t n, k; cin >> n >> k;
    auto a = vec<int64_t>(uns, n);
    for (auto &e : a) cin >> e;

    if (k == 0) {
        int64_t now = 0;
        for (int i = 0; i < n; ++i) {
            now -= abs(a[0] - a[i]);
        }

        int64_t ans = now;
        for (int i = 1; i < n; ++i) {
            auto d = a[i] - a[i - 1];
            now -= d * i;
            now += d * (n - i);
            ans = min(ans, now);
        }

        cout << ans << endl;
        return 0;
    }

    int64_t now = 0;
    for (int i = 0; i < n; ++i) {
        if (i < k) {
            now += abs(a[0] - a[i]);
        }
        else {
            now -= abs(a[0] - a[i]);
        }
    }

    int64_t ans = now;

    int l = 0, r = k;
    for (int i = 1; i < n; ++i) {
        auto d = a[i] - a[i - 1];
        auto pl = l, pr = r;
        while (r <= i || (r < n && (abs(a[i] - a[r]) < abs(a[i] - a[l])))) {
            now -= abs(a[i - 1] - a[l]) * 2;
            now += abs(a[i - 1] - a[r]) * 2;
            ++l; ++r;
        }

        now -= d * l;
        now += d * (i - l);
        now -= d * (r - i);
        now += d * (n - r);

        ans = min(ans, now);
    }

    cout << ans << endl;
}

0