結果

問題 No.1391 ±1 Abs Sum
ユーザー auauaauaua
提出日時 2021-02-12 23:12:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,701 bytes
コンパイル時間 1,677 ms
コンパイル使用メモリ 167,568 KB
実行使用メモリ 6,492 KB
最終ジャッジ日時 2023-09-27 06:32:00
合計ジャッジ時間 4,817 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
4,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 48 ms
4,920 KB
testcase_15 WA -
testcase_16 AC 64 ms
5,496 KB
testcase_17 WA -
testcase_18 AC 76 ms
5,900 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 55 ms
5,728 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 83 ms
6,192 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 88 ms
6,440 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=0;
        rep(i,n)ans-=a[i];
        cout<<ans<<endl;
        return 0;
    }else if(k==n){
        ll ans=0;
        rep(i,n)ans+=a[i];
        cout<<ans<<endl;
        return 0;
    }
    ll r=k,l=0;
    ll now=0;
    rep(i,n){
        if(i<k)now+=a[i];
        else now-=a[i];
    }
    ll ans=now;
    rep(i,n){
        while(r<n&&a[r]-a[i]<a[i]-a[l]){
            l++;
            r++;
            if(l>0)ans=min(ans,-(s[n-1]-s[r-1])+a[i]*(n-r)+s[r-1]-s[i]-a[i]*(r-1-i)+a[i]*(i-l+1)-(s[i]-s[l-1])+s[l-1]-a[i]*l);
            else ans=min(ans,-s[i]+s[r-1]-s[i]-(s[n-1]-s[r-1])+a[i]*(n-r+i-(r-1-i)));
        }
        //if(l>0)ans=min(ans,-(s[n-1]-s[r-1])+a[i]*(n-r)+s[r-1]-s[i]-a[i]*(r-1-i)+a[i]*(i-l+1)-(s[i]-s[l-1])+s[l-1]-a[i]*l);
        //else ans=min(ans,-s[i]+s[r-1]-s[i]-(s[n-1]-s[r-1])+a[i]*(n-r+i-(r-1-i)));
    }
    cout<<ans<<endl;
}
0