結果

問題 No.1391 ±1 Abs Sum
ユーザー merom686merom686
提出日時 2021-02-13 16:10:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 945 bytes
コンパイル時間 1,180 ms
コンパイル使用メモリ 88,588 KB
実行使用メモリ 5,476 KB
最終ジャッジ日時 2023-09-28 00:37:41
合計ジャッジ時間 3,759 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 26 ms
5,476 KB
testcase_12 AC 21 ms
4,924 KB
testcase_13 AC 24 ms
5,084 KB
testcase_14 AC 17 ms
4,520 KB
testcase_15 AC 18 ms
4,448 KB
testcase_16 AC 21 ms
4,980 KB
testcase_17 AC 19 ms
4,808 KB
testcase_18 AC 25 ms
5,204 KB
testcase_19 AC 18 ms
4,636 KB
testcase_20 AC 26 ms
5,356 KB
testcase_21 AC 17 ms
4,852 KB
testcase_22 AC 18 ms
4,856 KB
testcase_23 AC 20 ms
5,232 KB
testcase_24 AC 19 ms
5,156 KB
testcase_25 AC 19 ms
5,152 KB
testcase_26 AC 20 ms
5,160 KB
testcase_27 AC 14 ms
4,388 KB
testcase_28 AC 14 ms
4,596 KB
testcase_29 AC 18 ms
4,892 KB
testcase_30 AC 16 ms
4,604 KB
testcase_31 AC 27 ms
5,380 KB
testcase_32 AC 14 ms
4,376 KB
testcase_33 AC 16 ms
4,384 KB
testcase_34 AC 24 ms
5,208 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 28 ms
5,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    vector<ll> s(n + 1, 0);
    for (int i = 0; i < n; i++) {
        s[i + 1] = s[i] + a[i];
    }

    ll r = 1LL << 60;
    int l = 0;
    for (int i = 0; i < n; i++) {
        ll x = a[i];
        while (l + k < n && abs(x - a[l]) > abs(x - a[l + k])) l++;
        ll t = 0;
        if (l < i) t += -(s[min(i, l + k)] - s[l]) + x * (min(i, l + k) - l);
        if (l + k > i) t += (s[l + k] - s[max(i, l)]) - x * (l + k - max(i, l));
        t *= 2;
        t -= (s[n] - s[i]) - x * (n - i) - (s[i] - s[0]) + x * i;
        r = min(r, t);
    }

    cout << r << endl;

    return 0;
}
0