結果

問題 No.1391 ±1 Abs Sum
ユーザー seiyu_kyoproseiyu_kyopro
提出日時 2021-02-13 13:23:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,077 bytes
コンパイル時間 3,766 ms
コンパイル使用メモリ 167,004 KB
実行使用メモリ 6,320 KB
最終ジャッジ日時 2023-09-27 21:46:30
合計ジャッジ時間 4,278 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 WA -
testcase_06 AC 1 ms
4,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 40 ms
4,816 KB
testcase_15 AC 44 ms
4,920 KB
testcase_16 AC 54 ms
5,648 KB
testcase_17 AC 47 ms
5,124 KB
testcase_18 AC 64 ms
5,860 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 70 ms
6,132 KB
testcase_32 AC 36 ms
4,376 KB
testcase_33 AC 41 ms
4,376 KB
testcase_34 WA -
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 76 ms
6,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
const ll inf = 4611686018427387903LL;
const int MOD=998244353;
int main()
{
  int N,K;
  cin >> N >> K;
  vector<ll>a(N);
  rep(i,N)cin >> a[i];
  if(K==0){
    ll s1 = 0,s2=0;
    rep(i,N){
      s1+=abs(a[i]-a[0]);
      s2+=abs(a[i]-a[N-1]);
    }
    cout << min(-s1,-s2) << endl;
    return 0;
  }
  vector<ll>imos(N+1);
  imos[0]=0;
  rep(i,N)imos[i+1]=imos[i]+a[i];
  ll ans = inf;

  for(int L=0;L+K-1<N;L++){
    int R = L + K;
    // [L, R) を + にする

    int minus = L, plus = (N-R);
    int idx;
    if(plus-minus+K-1<=0)idx = R-1;
    // else (plus-minus-(K-1)>=0)idx=L;
    else idx=L;

    ll sum = 0, X = a[idx];
    //[0, L)について
    sum+= -L*X + imos[L];
    //[L, idx+1)について
    sum+= (idx+1-L)*X - (imos[idx+1]-imos[L]);
    //[idx+1, R)について
    sum+= -(K-(idx+1-L))*X + (imos[R]-imos[idx+1]);
    //[R, N)について
    sum+= (N-R)*X - (imos[N]-imos[R]);

    ans=min(ans,sum);
  }
  cout << ans << endl;
  return 0;
}
0