結果

問題 No.1391 ±1 Abs Sum
ユーザー 👑 hitonanodehitonanode
提出日時 2021-02-12 21:39:36
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 869 bytes
コンパイル時間 2,057 ms
コンパイル使用メモリ 201,652 KB
実行使用メモリ 6,204 KB
最終ジャッジ日時 2023-09-27 03:18:58
合計ジャッジ時間 4,945 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 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 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 30 ms
6,204 KB
testcase_12 AC 23 ms
5,412 KB
testcase_13 AC 28 ms
5,816 KB
testcase_14 AC 19 ms
4,884 KB
testcase_15 AC 20 ms
4,952 KB
testcase_16 AC 24 ms
5,396 KB
testcase_17 AC 22 ms
5,280 KB
testcase_18 AC 28 ms
5,892 KB
testcase_19 AC 20 ms
5,196 KB
testcase_20 AC 29 ms
5,892 KB
testcase_21 AC 20 ms
5,392 KB
testcase_22 AC 20 ms
5,404 KB
testcase_23 AC 23 ms
5,920 KB
testcase_24 AC 21 ms
5,672 KB
testcase_25 AC 22 ms
5,568 KB
testcase_26 AC 23 ms
5,984 KB
testcase_27 AC 16 ms
4,868 KB
testcase_28 AC 16 ms
5,128 KB
testcase_29 AC 21 ms
5,660 KB
testcase_30 AC 18 ms
5,132 KB
testcase_31 AC 32 ms
6,076 KB
testcase_32 AC 16 ms
4,552 KB
testcase_33 AC 18 ms
4,868 KB
testcase_34 AC 28 ms
5,832 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 31 ms
6,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)


int main() {
    int N, K;
    cin >> N >> K;
    vector<lint> A(N);
    for (auto &a : A) cin >> a;
    vector<lint> cum(N + 1);
    REP(i, N) cum[i + 1] = cum[i] + A[i];
    lint ret = 1LL << 60;

    int l = 0, r = K;
    REP(m, N) {
        if (m == r) l++, r++;
        while (r < N and abs(A[m] - A[l]) > abs(A[m] - A[r])) l++, r++;
        lint tmp = A[m] * (m - l) - (cum[m] - cum[l]) + (cum[r] - cum[m]) - A[m] * (r - m);
        tmp -= (cum[N] - cum[r]) - A[m] * (N - r) + A[m] * l - cum[l];
        ret = min(ret, tmp);
    }
    cout << ret << '\n';
}
0