結果

問題 No.1391 ±1 Abs Sum
ユーザー auauaauaua
提出日時 2021-02-13 13:52:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 1,619 bytes
コンパイル時間 1,547 ms
コンパイル使用メモリ 167,640 KB
実行使用メモリ 6,204 KB
最終ジャッジ日時 2023-09-27 22:16:42
合計ジャッジ時間 5,486 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 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 76 ms
6,128 KB
testcase_12 AC 57 ms
5,340 KB
testcase_13 AC 70 ms
5,992 KB
testcase_14 AC 45 ms
4,932 KB
testcase_15 AC 48 ms
4,948 KB
testcase_16 AC 59 ms
5,400 KB
testcase_17 AC 53 ms
5,276 KB
testcase_18 AC 70 ms
5,972 KB
testcase_19 AC 51 ms
5,260 KB
testcase_20 AC 76 ms
5,936 KB
testcase_21 AC 47 ms
5,460 KB
testcase_22 AC 51 ms
5,428 KB
testcase_23 AC 56 ms
6,204 KB
testcase_24 AC 55 ms
5,756 KB
testcase_25 AC 54 ms
5,792 KB
testcase_26 AC 52 ms
5,960 KB
testcase_27 AC 36 ms
5,128 KB
testcase_28 AC 38 ms
4,812 KB
testcase_29 AC 48 ms
5,792 KB
testcase_30 AC 41 ms
5,136 KB
testcase_31 AC 74 ms
6,152 KB
testcase_32 AC 36 ms
4,596 KB
testcase_33 AC 41 ms
4,976 KB
testcase_34 AC 67 ms
5,872 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 77 ms
6,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll mod=998244353;
const ll MAX=140010;



signed main(){
    ll n,k;cin>>n>>k;
    vector<ll>a(n);
    vector<ll>s(n);

    rep(i,n)cin>>a[i];
    //for(int i=n-1;i>=0;i--)a[i]-=a[0];
    s[0]=a[0];
    REP(i,1,n){
        s[i]=s[i-1]+a[i];
    }
    if(k==0){
        ll ans=inf;
        rep(i,n){
            ans=min(ans,-(s[n-1]-s[i]-a[i]*(n-i-1))-(-s[i]+a[i]*(i+1)));
        }
        cout<<ans<<endl;
        return 0;
    }else if(k==n){
        ll ans=inf;
        rep(i,n){
            ans=min(ans,(s[n-1]-s[i]-a[i]*(n-i-1))+(-s[i]+a[i]*(i+1)));
        }
        cout<<ans<<endl;
        return 0;
    }
    ll l=0;
    ll ans=inf;
    rep(i,n){
        while(l+k<n&&a[i]-a[l]>a[l+k]-a[i]&&i<=l+k){
            l++;
        }
        if(l)ans=min(ans,-(s[n-1]-s[l+k-1]-a[i]*(n-l-k))+(s[l+k-1]-s[i]-a[i]*(l+k-1-i))+(-s[i]+s[l-1]+a[i]*(i-l+1))-(a[i]*l-s[l-1]));
        else ans=min(ans,-(s[n-1]-s[k-1]-a[i]*(n-k))+(s[k-1]-s[i]-a[i]*(k-1-i))+(-s[i]+a[i]*(i+1)));
        //cout<<l<<' '<<l+k<<' '<<ans<<endl;
    }
    cout<<ans<<endl;
}
0