結果

問題 No.738 平らな農地
ユーザー しらっ亭しらっ亭
提出日時 2017-08-31 18:25:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 2,909 bytes
コンパイル時間 2,063 ms
コンパイル使用メモリ 177,416 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2024-04-24 09:16:41
合計ジャッジ時間 6,939 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 67 ms
6,656 KB
testcase_16 AC 67 ms
6,528 KB
testcase_17 AC 75 ms
5,760 KB
testcase_18 AC 65 ms
5,888 KB
testcase_19 AC 62 ms
5,376 KB
testcase_20 AC 67 ms
6,784 KB
testcase_21 AC 70 ms
5,760 KB
testcase_22 AC 70 ms
6,400 KB
testcase_23 AC 66 ms
5,504 KB
testcase_24 AC 62 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 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 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 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 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 75 ms
7,552 KB
testcase_46 AC 69 ms
6,784 KB
testcase_47 AC 74 ms
7,424 KB
testcase_48 AC 70 ms
7,424 KB
testcase_49 AC 68 ms
7,552 KB
testcase_50 AC 71 ms
7,808 KB
testcase_51 AC 73 ms
7,040 KB
testcase_52 AC 67 ms
7,168 KB
testcase_53 AC 69 ms
7,168 KB
testcase_54 AC 73 ms
7,296 KB
testcase_55 AC 74 ms
7,040 KB
testcase_56 AC 70 ms
7,168 KB
testcase_57 AC 67 ms
6,912 KB
testcase_58 AC 70 ms
7,296 KB
testcase_59 AC 73 ms
6,656 KB
testcase_60 AC 72 ms
6,784 KB
testcase_61 AC 70 ms
7,552 KB
testcase_62 AC 68 ms
7,168 KB
testcase_63 AC 80 ms
7,424 KB
testcase_64 AC 76 ms
6,400 KB
testcase_65 AC 11 ms
5,376 KB
testcase_66 AC 10 ms
5,376 KB
testcase_67 AC 42 ms
6,016 KB
testcase_68 AC 43 ms
6,272 KB
testcase_69 AC 49 ms
6,144 KB
testcase_70 AC 52 ms
7,680 KB
testcase_71 AC 23 ms
5,376 KB
testcase_72 AC 31 ms
5,376 KB
testcase_73 AC 31 ms
5,888 KB
testcase_74 AC 30 ms
5,376 KB
testcase_75 AC 50 ms
6,272 KB
testcase_76 AC 49 ms
7,424 KB
testcase_77 AC 49 ms
5,760 KB
testcase_78 AC 54 ms
6,016 KB
testcase_79 AC 55 ms
6,272 KB
testcase_80 AC 54 ms
7,296 KB
testcase_81 AC 51 ms
6,144 KB
testcase_82 AC 49 ms
5,760 KB
testcase_83 AC 30 ms
5,376 KB
testcase_84 AC 31 ms
5,376 KB
testcase_85 AC 62 ms
5,888 KB
testcase_86 AC 59 ms
5,504 KB
testcase_87 AC 60 ms
8,448 KB
testcase_88 AC 56 ms
8,192 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 operator+(const V &l, const V &r) {
  return {l.first + r.first, l.second + r.second};
}

V operator-(const V &l, const V &r) {
  return {l.first - r.first, l.second - r.second};
}

template <typename T> struct BinaryIndexedTree0 {
  const int N;
  vector<T> val;

  BinaryIndexedTree0(int N) : N(N), val(N) {}

  T sum(int i) const {
    T s = {0, 0};
    --i;
    if (i >= N) i = N - 1;
    for (; i >= 0; i = (i & (i + 1)) - 1) s = s + val[i];
    return s;
  }

  T sum(int l, int r) const {
    return sum(r) - sum(l);
  }

  void add(int i, T x) {
    for (; i < N; i |= i + 1) val[i] = val[i] + x;
  }

  void set(int i, T x) {
    T c = sum(i, i + 1);
    add(i, x - c);
  }
};

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

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

  int64_t ans = 1e18;

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

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

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

      // 区間内で median 未満の個数と総和を取得
      V lower = ft.sum(median_c);
      // 区間内で median 以上の個数と総和を取得
      V higher = ft.sum(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