結果

問題 No.1391 ±1 Abs Sum
ユーザー chocoruskchocorusk
提出日時 2021-02-12 21:49:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,151 bytes
コンパイル時間 1,093 ms
コンパイル使用メモリ 125,960 KB
実行使用メモリ 6,668 KB
最終ジャッジ日時 2023-09-27 03:40:04
合計ジャッジ時間 4,418 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 83 ms
6,516 KB
testcase_12 AC 62 ms
5,708 KB
testcase_13 AC 76 ms
6,488 KB
testcase_14 AC 49 ms
5,092 KB
testcase_15 AC 50 ms
5,208 KB
testcase_16 AC 63 ms
5,680 KB
testcase_17 AC 56 ms
5,456 KB
testcase_18 AC 77 ms
6,148 KB
testcase_19 AC 55 ms
5,728 KB
testcase_20 AC 80 ms
6,360 KB
testcase_21 AC 52 ms
5,932 KB
testcase_22 AC 52 ms
5,804 KB
testcase_23 AC 61 ms
6,244 KB
testcase_24 AC 56 ms
6,336 KB
testcase_25 AC 56 ms
6,104 KB
testcase_26 AC 60 ms
6,196 KB
testcase_27 AC 40 ms
5,312 KB
testcase_28 AC 42 ms
5,452 KB
testcase_29 AC 55 ms
6,000 KB
testcase_30 AC 46 ms
5,564 KB
testcase_31 AC 84 ms
6,668 KB
testcase_32 AC 42 ms
4,932 KB
testcase_33 AC 49 ms
5,224 KB
testcase_34 AC 78 ms
6,312 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 88 ms
6,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <stack>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
ll a[200020];
ll s[200020];
int main()
{
    int n, k; cin>>n>>k;
    for(int i=0; i<n; i++){
        cin>>a[i];
        s[i+1]=s[i]+a[i];
    }
    ll ans=0;
    for(int i=0; i<n; i++){
        if(i<k) ans+=abs(a[0]-a[i]);
        else ans-=abs(a[0]-a[i]);
    }
    int l=0;
    for(int i=1; i<n; i++){
        ll t=a[i]*(i-l)-(s[i]-s[l])+s[l+k]-s[i]-a[i]*(l+k-i);
        while(l+k<n && t>a[i]*(i-l-1)-(s[i]-s[l+1])+s[l+k+1]-s[i]-a[i]*(l+k+1-i)){
            l++;
            t=a[i]*(i-l)-(s[i]-s[l])+s[l+k]-s[i]-a[i]*(l+k-i);
        }
        ans=min(ans, t*2-(a[i]*i-s[i]-a[i]*(n-i)+(s[n]-s[i])));
    }
    cout<<ans<<endl;
    return 0;
}
0