結果

問題 No.738 平らな農地
ユーザー しらっ亭しらっ亭
提出日時 2017-05-30 03:42:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,137 bytes
コンパイル時間 1,910 ms
コンパイル使用メモリ 169,836 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-21 14:53:33
合計ジャッジ時間 5,661 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 WA -
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 15 ms
5,376 KB
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 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 AC 12 ms
5,376 KB
testcase_66 AC 11 ms
5,376 KB
testcase_67 WA -
testcase_68 AC 16 ms
5,376 KB
testcase_69 WA -
testcase_70 WA -
testcase_71 AC 9 ms
5,376 KB
testcase_72 AC 16 ms
5,376 KB
testcase_73 AC 15 ms
5,376 KB
testcase_74 AC 17 ms
5,376 KB
testcase_75 AC 16 ms
5,376 KB
testcase_76 AC 18 ms
5,376 KB
testcase_77 AC 14 ms
5,376 KB
testcase_78 AC 16 ms
5,376 KB
testcase_79 AC 17 ms
5,376 KB
testcase_80 AC 17 ms
5,376 KB
testcase_81 AC 16 ms
5,376 KB
testcase_82 AC 15 ms
5,376 KB
testcase_83 AC 13 ms
5,376 KB
testcase_84 AC 14 ms
5,376 KB
testcase_85 AC 15 ms
5,376 KB
testcase_86 AC 14 ms
5,376 KB
testcase_87 AC 21 ms
5,376 KB
testcase_88 AC 22 ms
5,376 KB
testcase_89 AC 2 ms
5,376 KB
testcase_90 AC 3 ms
5,376 KB
testcase_91 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int64_t solve(const int n, const int k, const vector<int> &A) {
  if (k == 1) return 0;

  int64_t sum = 0;
  __int128_t sum2 = 0;

  double min_var = 1e20;
  int min_var_idx = -1;

  for (int i = 0; i < n; i++) {
    sum += A[i];
    sum2 += 1LL * A[i] * A[i];

    // 範囲外に出たものを取り除く
    if (i >= k) {
      sum -= A[i-k];
      sum2 -= 1LL * A[i-k] * A[i-k];
    }
    if (i >= k - 1) {
      // k 要素の分散を求める
      double avg = 1.0 * sum / k;
      double variance = 1.0 * sum2 / k - avg * avg;
      if (min_var > variance) {
        min_var = variance;
        min_var_idx = i;
      }
    }
  }

  vector<int> B;
  for (int i = 0; i < k; i++) {
    B.emplace_back(A[min_var_idx - i]);
  }

  sort(B.begin(), B.end());

  int median = B[(k+1)/2-1];

  int64_t ans = 0;
  for (int b : B) {
    ans += abs(b - median);
  }

  return ans;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, k; cin >> n >> k;
  vector<int> A(n);
  for (int i = 0; i < n; i++) cin >> A[i];

  cout << solve(n, k, A) << endl;

  return 0;
}
0