結果

問題 No.1391 ±1 Abs Sum
ユーザー Ricky_ponRicky_pon
提出日時 2021-02-16 19:30:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 1,563 bytes
コンパイル時間 2,168 ms
コンパイル使用メモリ 198,248 KB
実行使用メモリ 6,780 KB
最終ジャッジ日時 2023-10-11 09:59:18
合計ジャッジ時間 5,072 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 30 ms
6,644 KB
testcase_12 AC 23 ms
6,020 KB
testcase_13 AC 27 ms
6,396 KB
testcase_14 AC 19 ms
5,228 KB
testcase_15 AC 19 ms
5,376 KB
testcase_16 AC 23 ms
5,928 KB
testcase_17 AC 21 ms
5,684 KB
testcase_18 AC 28 ms
6,344 KB
testcase_19 AC 20 ms
5,596 KB
testcase_20 AC 30 ms
6,780 KB
testcase_21 AC 21 ms
6,000 KB
testcase_22 AC 21 ms
6,004 KB
testcase_23 AC 23 ms
6,480 KB
testcase_24 AC 22 ms
6,184 KB
testcase_25 AC 22 ms
6,228 KB
testcase_26 AC 23 ms
6,336 KB
testcase_27 AC 17 ms
5,444 KB
testcase_28 AC 17 ms
5,520 KB
testcase_29 AC 21 ms
6,132 KB
testcase_30 AC 19 ms
5,680 KB
testcase_31 AC 31 ms
6,672 KB
testcase_32 AC 16 ms
5,072 KB
testcase_33 AC 18 ms
5,304 KB
testcase_34 AC 28 ms
6,428 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 32 ms
6,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int main() {
    int n, K;
    scanf("%d%d", &n, &K);
    lint a[n], asum[n + 1];
    asum[0] = 0;
    rep(i, n) scanf("%lld", &a[i]), asum[i + 1] = asum[i] + a[i];

    int l = 0, r = K;
    lint ans = INF;
    rep(i, n) {
        while (r < n && a[r] - a[i] < a[i] - a[l]) {
            ++r;
            ++l;
        }
        lint tmp = a[i] * (n - r + i - l);
        tmp -= a[i] * (r - i + l);
        tmp += asum[r] - asum[i] + asum[l];
        tmp -= asum[n] - asum[r] + asum[i] - asum[l];
        chmin(ans, tmp);
    }

    printf("%lld\n", ans);
}
0