結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 80 ms
4,580 KB
testcase_12 AC 61 ms
4,380 KB
testcase_13 AC 73 ms
4,380 KB
testcase_14 AC 47 ms
4,376 KB
testcase_15 AC 49 ms
4,380 KB
testcase_16 AC 61 ms
4,380 KB
testcase_17 AC 55 ms
4,380 KB
testcase_18 AC 74 ms
4,376 KB
testcase_19 AC 54 ms
4,380 KB
testcase_20 AC 78 ms
4,768 KB
testcase_21 AC 50 ms
4,376 KB
testcase_22 AC 50 ms
4,380 KB
testcase_23 AC 58 ms
4,404 KB
testcase_24 AC 54 ms
4,380 KB
testcase_25 AC 55 ms
4,380 KB
testcase_26 AC 57 ms
4,428 KB
testcase_27 AC 39 ms
4,376 KB
testcase_28 AC 40 ms
4,384 KB
testcase_29 AC 54 ms
4,380 KB
testcase_30 AC 45 ms
4,376 KB
testcase_31 AC 81 ms
4,800 KB
testcase_32 AC 41 ms
4,380 KB
testcase_33 AC 47 ms
4,380 KB
testcase_34 AC 75 ms
4,580 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 86 ms
4,812 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){
            // now += (i - l + 1 + N - r - (l + (r - i - 1))) * A[i + 1];
            // now += -2 * A[i + 1];
            //次のx
            now += (i + 1 - l + 1 + N - 1 - r - (l + (r - (i + 1)))) * a[i + 1];
            // now -= (i - l + 1 + N - 1 - r - (l + (r - i))) * a[i + 1];
            // now += (i - l + 1 + N - r - (l + (r - i - 1))) * a[i + 1];
            now += -2 * a[i + 1];
        }
    }

    cout << ans << endl;

    return 0;
}
0