結果

問題 No.738 平らな農地
ユーザー 0w10w1
提出日時 2018-10-10 02:51:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,608 bytes
コンパイル時間 4,680 ms
コンパイル使用メモリ 266,148 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-20 19:22:56
合計ジャッジ時間 9,249 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,368 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 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class Node_CItr, class Node_Itr, class Cmp_Fn, class _Alloc>
struct node_update {
  typedef pair<int, int64_t> metadata_type; // (size, sum)
  Node_Itr find_by_order(int s) const {
    auto it = node_begin();
    while (it != node_end()) {
      auto l = it.get_l_child();
      auto r = it.get_r_child();
      int lsize = l == node_end() ? 0 : l.get_metadata().first;
      if (s < lsize) it = l;
      else if (s < lsize + (*it)->second) return it;
      else s -= lsize + (*it)->second, it = r;
    }
    return it;
  }
  int get_size() const { return node_begin().get_metadata().first; }
  int64_t get_sum() const { return node_begin().get_metadata().second; }
  void operator()(Node_Itr it, Node_CItr end_it) {
    auto l = it.get_l_child();
    auto r = it.get_r_child();
    pair<int, int64_t> left, right;
    if (l != end_it) left = l.get_metadata();
    if (r != end_it) right = r.get_metadata();
    const_cast<pair<int, int64_t> &>(it.get_metadata()) = make_pair(
        left.first + right.first + (*it)->second, left.second + right.second + 1LL * (*it)->first * (*it)->second);
  }
  virtual Node_CItr node_begin() const = 0;
  virtual Node_CItr node_end() const = 0;
};

typedef tree<pair<int, int>, null_type, less<pair<int, int64_t>>, rb_tree_tag, node_update> rbtree;

signed main() {
  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];

  rbtree t;
  for (int i = 0; i < K; ++i) {
    auto it = t.lower_bound({A[i], 0LL});
    if (it == t.end() || it->first != A[i]) {
      t.insert({A[i], 1LL});
    } else {
      t.insert({A[i], it->second + 1});
      t.erase(t.lower_bound({A[i], 0LL}));
    }
  }

  auto eval = [&](rbtree &t) {
    auto med = **t.find_by_order(t.get_size() / 2);
    rbtree right;
    t.split(med, right);
    int64_t res = 0;
    if (t.size()) res += 1LL * med.first * t.get_size() - t.get_sum();
    if (right.size()) res += right.get_sum() - 1LL * med.first * right.get_size();
    t.join(right);
    return res;
  };

  int64_t ans = eval(t);
  for (int i = 1; i + K <= N; ++i) {
    auto it = t.lower_bound({A[i - 1], 0LL});
    if (it->second > 1) t.insert({it->first, it->second - 1});
    else t.erase(it);
    it = t.lower_bound({A[i + K - 1], 0LL});
    if (it == t.end() || it->first != A[i + K - 1]) t.insert({A[i + K - 1], 1LL});
    else t.insert({it->first, it->second + 1}), t.erase({A[i + K - 1], 1LL});
    ans = min(ans, eval(t));
  }

  cout << ans << endl;
}
0