結果

問題 No.1391 ±1 Abs Sum
ユーザー seiyu_kyoproseiyu_kyopro
提出日時 2021-02-13 15:14:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,264 bytes
コンパイル時間 1,740 ms
コンパイル使用メモリ 167,624 KB
実行使用メモリ 4,772 KB
最終ジャッジ日時 2023-09-27 23:42:17
合計ジャッジ時間 4,398 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 41 ms
4,380 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 2 ms
4,376 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

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 - 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