結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 1 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,384 KB
testcase_08 WA -
testcase_09 AC 2 ms
4,380 KB
testcase_10 WA -
testcase_11 AC 78 ms
4,744 KB
testcase_12 AC 59 ms
4,380 KB
testcase_13 AC 71 ms
4,376 KB
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 77 ms
4,572 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 73 ms
4,628 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 83 ms
4,572 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));

            ans = min(ans, cnt);
            prev = cnt;
        }

        return ans;
    }();

    cout << ans << endl;
}

0