結果

問題 No.1391 ±1 Abs Sum
ユーザー sten_sansten_san
提出日時 2021-02-13 02:00:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,007 bytes
コンパイル時間 2,043 ms
コンパイル使用メモリ 202,092 KB
実行使用メモリ 4,844 KB
最終ジャッジ日時 2023-09-27 09:16:00
合計ジャッジ時間 6,225 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
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 AC 78 ms
4,560 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 84 ms
4,744 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;

    auto pos = [&]() {
        int64_t prev = 0;
        for (int i = 0; i < k; ++i) {
            prev += abs(a[i] - a[k / 2]);
        }

        int pos = k / 2;
        int64_t sub = prev;
        for (int i = k / 2 + 1; i < n - (k - 1) / 2; ++i) {
            auto cnt = prev;
            cnt -= abs(a[i - k / 2 - 1] - a[i - 1]);
            cnt += abs(a[i + (k - 1) / 2] - a[i]);
            cnt += abs(a[i] - a[i - 1]) * (k / 2);
            cnt -= abs(a[i] - a[i - 1]) * ((k - 1) / 2);

            if (cnt < sub) {
                pos = i;
                sub = cnt;
            }

            prev = cnt;
        }

        return pos;
    }();

    auto ans = [&]() {
        int64_t prev = 0;
        for (int i = 0; i < n; ++i) {
            if (pos - k / 2 <= i && i < pos + (k + 1) / 2) {
                prev += abs(a[i] - a[pos - k / 2]);
            }
            else {
                prev -= abs(a[i] - a[pos - k / 2]);
            }
        }

        int64_t ans = prev;
        for (int i = pos - k / 2 + 1; i < pos + (k + 1) / 2; ++i) {
            auto cnt = prev;
            auto d = a[i] - a[i - 1];
            cnt -= d * (pos - k / 2);
            cnt += d * (i - (pos - k / 2));
            cnt -= d * (pos + (k + 1) / 2 - i);
            cnt += d * (n - (pos + (k + 1) / 2) + 1);

            ans = min(ans, cnt);

            prev = cnt;
        }

        return ans;
    }();

    cout << ans << endl;
}

0