結果

問題 No.1391 ±1 Abs Sum
ユーザー seiyu_kyoproseiyu_kyopro
提出日時 2021-02-13 15:08:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 1,584 bytes
コンパイル時間 3,953 ms
コンパイル使用メモリ 164,868 KB
実行使用メモリ 4,736 KB
最終ジャッジ日時 2023-09-27 23:35:38
合計ジャッジ時間 5,675 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 73 ms
4,728 KB
testcase_12 AC 55 ms
4,376 KB
testcase_13 AC 66 ms
4,412 KB
testcase_14 AC 42 ms
4,380 KB
testcase_15 AC 43 ms
4,380 KB
testcase_16 AC 56 ms
4,376 KB
testcase_17 AC 48 ms
4,376 KB
testcase_18 AC 66 ms
4,468 KB
testcase_19 AC 47 ms
4,376 KB
testcase_20 AC 71 ms
4,736 KB
testcase_21 AC 45 ms
4,380 KB
testcase_22 AC 46 ms
4,468 KB
testcase_23 AC 52 ms
4,380 KB
testcase_24 AC 48 ms
4,468 KB
testcase_25 AC 50 ms
4,480 KB
testcase_26 AC 51 ms
4,472 KB
testcase_27 AC 35 ms
4,376 KB
testcase_28 AC 37 ms
4,376 KB
testcase_29 AC 47 ms
4,436 KB
testcase_30 AC 40 ms
4,380 KB
testcase_31 AC 73 ms
4,544 KB
testcase_32 AC 37 ms
4,376 KB
testcase_33 AC 42 ms
4,380 KB
testcase_34 AC 68 ms
4,540 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 78 ms
4,552 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 を一つ進める
        // now += l * a[i];
        // now -= max(0, i - l) * a[i];
        // now += max(0, r - (i + 1) + 1 ) * a[i];
        // now -= max(0, N - 1 - (r + 1) + 1) * a[i];
        //xの値を入れ替える
        // [0, l-1]、[i+1, r]ではxの符号は +、それ以外では -
        now -= (i - l + 1 + N - 1 - r - (l + (r - i))) * a[i];
        if(i + 1 < N){
            // now -= l * a[i + 1];
            // now += max(0, i - l) * a[i + 1];
            // now -= max(0, r - (i + 1 + 1) + 1 ) * a[i + 1];
            // now += max(0, N - 1 - (r + 1) + 1) * a[i+ 1];
            //次のx
            now += (i - l + 1 + N - r - (l + (r - i - 1))) * a[i + 1];
            now += -2 * a[i + 1];
        }
    }

    cout << ans << endl;

    return 0;
}
0