結果

問題 No.1391 ±1 Abs Sum
ユーザー SSRSSSRS
提出日時 2021-02-12 22:19:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 1,005 bytes
コンパイル時間 1,423 ms
コンパイル使用メモリ 168,956 KB
実行使用メモリ 5,548 KB
最終ジャッジ日時 2023-09-27 04:42:29
合計ジャッジ時間 4,618 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 74 ms
5,548 KB
testcase_12 AC 55 ms
4,888 KB
testcase_13 AC 66 ms
5,104 KB
testcase_14 AC 41 ms
4,376 KB
testcase_15 AC 43 ms
4,380 KB
testcase_16 AC 55 ms
4,380 KB
testcase_17 AC 48 ms
4,376 KB
testcase_18 AC 66 ms
4,380 KB
testcase_19 AC 49 ms
4,688 KB
testcase_20 AC 72 ms
5,100 KB
testcase_21 AC 43 ms
4,948 KB
testcase_22 AC 46 ms
4,840 KB
testcase_23 AC 52 ms
5,280 KB
testcase_24 AC 48 ms
4,840 KB
testcase_25 AC 52 ms
5,148 KB
testcase_26 AC 52 ms
5,212 KB
testcase_27 AC 35 ms
4,376 KB
testcase_28 AC 37 ms
4,936 KB
testcase_29 AC 48 ms
4,896 KB
testcase_30 AC 40 ms
4,748 KB
testcase_31 AC 70 ms
4,376 KB
testcase_32 AC 37 ms
4,380 KB
testcase_33 AC 42 ms
4,376 KB
testcase_34 AC 67 ms
5,100 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 78 ms
5,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000;
int main(){
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  if (K <= N - K){
    long long ans1 = 0, ans2 = 0;
    for (int i = 0; i < K; i++){
      ans1 += A[i] - A[0];
      ans2 += A[N - 1] - A[N - 1 - i];
    }
    for (int i = K; i < N; i++){
      ans1 -= A[i] - A[0];
      ans2 -= A[N - 1] - A[N - 1 - i];
    }
    cout << min(ans1, ans2) << endl;
  } else {
    vector<long long> S(N + 1);
    S[0] = 0;
    for (int i = 0; i < N; i++){
      S[i + 1] = S[i] + A[i];
    }
    long long ans = INF;
    for (int i = 0; i <= N - K; i++){
      long long p = (2 * K - N) / 2 + i * 2;
      long long x = A[p];
      long long tmp = 0;
      tmp += S[i] - S[0];
      tmp -= S[p] - S[i];
      tmp += S[i + K] - S[p];
      tmp -= S[N] - S[i + K];
      tmp += (N + p * 2 - i * 4 - K * 2) * x;
      ans = min(ans, tmp);
    }
    cout << ans << endl;
  }
}
0