結果

問題 No.738 平らな農地
ユーザー しらっ亭しらっ亭
提出日時 2017-08-31 18:16:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 2,970 bytes
コンパイル時間 2,022 ms
コンパイル使用メモリ 178,440 KB
実行使用メモリ 12,408 KB
最終ジャッジ日時 2024-04-24 09:16:19
合計ジャッジ時間 8,743 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 AC 3 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 87 ms
7,680 KB
testcase_16 AC 87 ms
7,680 KB
testcase_17 AC 87 ms
6,940 KB
testcase_18 AC 85 ms
6,912 KB
testcase_19 AC 82 ms
6,400 KB
testcase_20 AC 89 ms
7,680 KB
testcase_21 AC 93 ms
6,944 KB
testcase_22 AC 90 ms
7,552 KB
testcase_23 AC 88 ms
6,656 KB
testcase_24 AC 85 ms
6,528 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 3 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 3 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 95 ms
10,624 KB
testcase_46 AC 88 ms
7,936 KB
testcase_47 AC 92 ms
10,552 KB
testcase_48 AC 88 ms
8,448 KB
testcase_49 AC 85 ms
8,448 KB
testcase_50 AC 92 ms
10,852 KB
testcase_51 AC 99 ms
10,136 KB
testcase_52 AC 90 ms
8,064 KB
testcase_53 AC 89 ms
10,344 KB
testcase_54 AC 92 ms
10,360 KB
testcase_55 AC 94 ms
9,996 KB
testcase_56 AC 93 ms
10,224 KB
testcase_57 AC 87 ms
8,192 KB
testcase_58 AC 92 ms
10,448 KB
testcase_59 AC 93 ms
10,752 KB
testcase_60 AC 96 ms
10,880 KB
testcase_61 AC 94 ms
10,664 KB
testcase_62 AC 87 ms
8,320 KB
testcase_63 AC 99 ms
10,240 KB
testcase_64 AC 96 ms
10,368 KB
testcase_65 AC 12 ms
5,376 KB
testcase_66 AC 12 ms
5,376 KB
testcase_67 AC 56 ms
10,028 KB
testcase_68 AC 57 ms
10,312 KB
testcase_69 AC 66 ms
8,780 KB
testcase_70 AC 68 ms
9,936 KB
testcase_71 AC 25 ms
5,376 KB
testcase_72 AC 34 ms
5,376 KB
testcase_73 AC 34 ms
5,888 KB
testcase_74 AC 32 ms
5,376 KB
testcase_75 AC 68 ms
8,912 KB
testcase_76 AC 64 ms
9,928 KB
testcase_77 AC 67 ms
8,528 KB
testcase_78 AC 75 ms
8,448 KB
testcase_79 AC 69 ms
8,832 KB
testcase_80 AC 69 ms
9,852 KB
testcase_81 AC 68 ms
8,872 KB
testcase_82 AC 66 ms
8,528 KB
testcase_83 AC 34 ms
5,376 KB
testcase_84 AC 36 ms
5,376 KB
testcase_85 AC 93 ms
8,320 KB
testcase_86 AC 87 ms
8,320 KB
testcase_87 AC 76 ms
12,408 KB
testcase_88 AC 74 ms
12,288 KB
testcase_89 AC 2 ms
5,376 KB
testcase_90 AC 2 ms
5,376 KB
testcase_91 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using V = pair<int, int64_t>;

V merge(const V &l, const V &r) {
  return {l.first + r.first, l.second + r.second};
}

struct SegmentTree {
  const int n;
  const V unit_value = {0, 0};
  vector<V> val;

  SegmentTree(int _n) : n(1 << (33-__builtin_clz(_n-1))), val(n, unit_value) {}

  void update(int i, const V &v) {
    i += n / 2;
    val[i] = merge(val[i], v);
    while (i > 1) {
      i >>= 1;
      val[i] = merge(val[i * 2 + 0], val[i * 2 + 1]);
    }
  }

  // [l, r)
  V query(int l, int r) const {
    l = max(0, min(n / 2, l)) + n / 2;
    r = max(0, min(n / 2, r)) + n / 2;
    V ret = unit_value;
    for (; l < r; l >>= 1, r >>= 1) {
      if (l & 1) ret = merge(ret, val[l++]);
      if (r & 1) ret = merge(ret, val[--r]);
    }
    return ret;
  }
};

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

  // 座圧の前準備
  vector<int> compressor(A);
  {
    sort(compressor.begin(), compressor.end());
    compressor.erase(unique(compressor.begin(), compressor.end()), compressor.end());
  }
  auto compress = [&compressor](int i) {
    return lower_bound(compressor.begin(), compressor.end(), i) - compressor.begin();
  };

  // 座圧した結果出てくる数字の種類数
  int m = compressor.size();

  // median と、その座圧版を全部求めておく
  int num_medians = n - k + 1;
  vector<int> medians(num_medians);
  {
    multiset<int> S(&A[0], &A[k]);
    auto it = next(S.begin(), k / 2);

    for (int i = k; i <= n; i++) {
      medians[i - k] = *it;
      S.insert(A[i]);
      if (A[i] <  *it) it--;
      if (A[i-k] <= *it) it++;
      S.erase(S.lower_bound(A[i-k]));
    }
  }

  // seg木の i 番目には、今注目している長さ k の区間で、座圧後に i になる値の、個数と合計を入れている。
  // その range-sum ができるようになっている
  SegmentTree seg(m);

  int64_t ans = 1e18;

  for (int i = 0; i < n; i++) {
    // 値を入れる O(log N)
    int a = A[i];
    seg.update(compress(a), {1, a});

    // 範囲外に出たものを取り除く O(log N)
    if (i >= k) {
      int c = A[i-k];
      seg.update(compress(c), {-1, -c});
    }

    if (i >= k - 1) {
      int64_t median = medians[i-k+1];
      int median_c = compress(median);

      // 区間内で median 未満の個数と総和を取得
      V lower = seg.query(0, median_c);
      // 区間内で median 以上の個数と総和を取得
      V higher = seg.query(median_c, m);

      int64_t inc_cost = median * lower.first - lower.second;
      int64_t dec_cost = higher.second - median * higher.first;
      int64_t cand = inc_cost + dec_cost;

      ans = min(ans, cand);
    }
  }
  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