結果

問題 No.1391 ±1 Abs Sum
ユーザー seiyu_kyoproseiyu_kyopro
提出日時 2021-02-13 15:27:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 1,197 bytes
コンパイル時間 1,356 ms
コンパイル使用メモリ 167,608 KB
実行使用メモリ 4,696 KB
最終ジャッジ日時 2023-09-27 23:55:31
合計ジャッジ時間 4,581 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 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,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 75 ms
4,628 KB
testcase_12 AC 56 ms
4,380 KB
testcase_13 AC 70 ms
4,572 KB
testcase_14 AC 43 ms
4,380 KB
testcase_15 AC 47 ms
4,380 KB
testcase_16 AC 60 ms
4,380 KB
testcase_17 AC 50 ms
4,376 KB
testcase_18 AC 69 ms
4,432 KB
testcase_19 AC 49 ms
4,376 KB
testcase_20 AC 72 ms
4,696 KB
testcase_21 AC 46 ms
4,376 KB
testcase_22 AC 45 ms
4,376 KB
testcase_23 AC 54 ms
4,380 KB
testcase_24 AC 49 ms
4,376 KB
testcase_25 AC 52 ms
4,376 KB
testcase_26 AC 53 ms
4,440 KB
testcase_27 AC 37 ms
4,376 KB
testcase_28 AC 38 ms
4,380 KB
testcase_29 AC 50 ms
4,380 KB
testcase_30 AC 44 ms
4,380 KB
testcase_31 AC 76 ms
4,544 KB
testcase_32 AC 39 ms
4,376 KB
testcase_33 AC 44 ms
4,380 KB
testcase_34 AC 71 ms
4,696 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 81 ms
4,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
const ll inf = 4611686018427387903LL;
const int MOD=998244353;
int main(){
    int N,K;
    cin >> N >> K;
    vector<ll> a(N);
    rep(i, N) cin >> a[i];
    
    int l = 0;
    int r = K - 1;
    //[l, r] を +1 にする
    
    ll ans = inf;
    ll now = 0;
    rep(i,N){
        if(l <= i && i <= r) now += abs(a[0] - a[i]);
        else now -= abs(a[0] - a[i]);
    }


    rep(i,N){
        // x = A[i]
        while(r < i || (r + 1 < N && abs(a[l] - a[i]) > abs(a[r + 1] - a[i]))){
            // 得をするので動かす
            now = now - 2 * abs(a[l] - a[i]) + 2 * abs(a[r + 1] - a[i]);
            l++;
            r++;
        }

        ans = min(ans, now);

        // i を一つ進める
        // [0, l-1]、[i+1, r]ではxを足して戻す
        // それ以外引いて戻す
        now -= (i - l + 1 + N - 1 - r - (l + (r - i))) * a[i];
        if(i + 1 < N){
            //次のx
            now += (i + 1 - l + 1 + N - 1 - r - (l + (r - (i + 1)))) * a[i + 1];
            now += -2 * a[i + 1];
        }
    }

    cout << ans << endl;

    return 0;
}
0