結果

問題 No.1391 ±1 Abs Sum
ユーザー 👑 deuteridayodeuteridayo
提出日時 2021-02-12 22:58:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,176 bytes
コンパイル時間 1,366 ms
コンパイル使用メモリ 164,980 KB
実行使用メモリ 5,104 KB
最終ジャッジ日時 2023-09-27 06:04:05
合計ジャッジ時間 4,645 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 WA -
testcase_05 AC 1 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 1 ms
4,380 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 81 ms
4,892 KB
testcase_12 AC 61 ms
4,516 KB
testcase_13 WA -
testcase_14 AC 46 ms
4,376 KB
testcase_15 WA -
testcase_16 AC 63 ms
4,508 KB
testcase_17 WA -
testcase_18 AC 74 ms
4,792 KB
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 AC 81 ms
4,964 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 77 ms
4,808 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 85 ms
4,952 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;
    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(abs(a[left] - center) > abs(a[right+1] - center)){
                left++;
                right++;
                plus -= abs(a[left] - center);
                plus += abs(a[right+1] - center);
            }else{
                break;
            }
        }


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


}

0