結果

問題 No.1391 ±1 Abs Sum
ユーザー 👑 deuteridayodeuteridayo
提出日時 2021-02-12 23:04:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,301 bytes
コンパイル時間 1,512 ms
コンパイル使用メモリ 166,464 KB
実行使用メモリ 4,960 KB
最終ジャッジ日時 2023-09-27 06:16:47
合計ジャッジ時間 4,721 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 82 ms
4,904 KB
testcase_12 AC 62 ms
4,532 KB
testcase_13 AC 75 ms
4,920 KB
testcase_14 AC 48 ms
4,380 KB
testcase_15 AC 50 ms
4,380 KB
testcase_16 AC 62 ms
4,524 KB
testcase_17 AC 55 ms
4,392 KB
testcase_18 AC 75 ms
4,752 KB
testcase_19 AC 54 ms
4,380 KB
testcase_20 AC 79 ms
4,852 KB
testcase_21 AC 51 ms
4,580 KB
testcase_22 AC 51 ms
4,688 KB
testcase_23 AC 59 ms
4,800 KB
testcase_24 AC 55 ms
4,688 KB
testcase_25 AC 55 ms
4,748 KB
testcase_26 AC 58 ms
4,768 KB
testcase_27 AC 39 ms
4,540 KB
testcase_28 AC 41 ms
4,376 KB
testcase_29 AC 54 ms
4,652 KB
testcase_30 AC 45 ms
4,464 KB
testcase_31 AC 82 ms
4,896 KB
testcase_32 AC 42 ms
4,384 KB
testcase_33 AC 48 ms
4,380 KB
testcase_34 AC 76 ms
4,828 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 86 ms
4,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
// #include<atcoder/all>
using namespace std;
// using namespace atcoder;
using lint = long long;
template<class T>using graph = vector<vector<T>>;
#define endl '\n'
int const INF = 1<<30;
lint const INF64 = 1LL<<62;
lint const mod = 1e9+7;
//long const mod = 998244353;


int main(){
    lint n,k;
    cin >> n >> k;
    lint a[n];
    for(int i=0;i<n;i++)cin >> a[i];
    lint summ = 0;
    lint plus = 0;
    for(int i=0;i<n;i++){
        summ += abs(a[i] - a[0]);
        if(i<k)plus += abs(a[i]-a[0]);
    }
    int left = 0;
    int right = k-1;
    if(k==0)plus = 0;
    lint ans = plus*2 - summ;
    for(lint i=1;i<n;i++){
        lint center = a[i];
        lint diff = a[i] - a[i-1];
        summ += (i - 1 - (n-1-i)) * diff;
        plus += (i - left - 1 - right + i) * diff;

        while(right < n-1){
            if(k==0)break;
            if(abs(a[left] - center) > abs(a[right+1] - center)){
                
                plus -= abs(a[left] - center);
                plus += abs(a[right+1] - center);
                left++;
                right++;
            }else{
                break;
            }
        }
        // cout << plus << endl;
        if(k==0)plus = 0;


        ans = min(ans, plus*2 - summ);
    }   
    cout << ans << endl;


}

0