結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 46 ms
5,008 KB
testcase_15 WA -
testcase_16 AC 61 ms
5,420 KB
testcase_17 WA -
testcase_18 AC 73 ms
5,948 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
6,128 KB
testcase_32 AC 42 ms
4,380 KB
testcase_33 AC 48 ms
4,380 KB
testcase_34 WA -
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 87 ms
6,260 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;
    idx = minus;

    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]);

    // cout << sum << endl;

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