結果

問題 No.738 平らな農地
ユーザー しらっ亭しらっ亭
提出日時 2017-05-30 03:41:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 8,887 bytes
コンパイル時間 2,666 ms
コンパイル使用メモリ 185,224 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2024-04-21 14:49:27
合計ジャッジ時間 11,325 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 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 4 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 3 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 2 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 118 ms
7,816 KB
testcase_16 AC 116 ms
7,808 KB
testcase_17 AC 118 ms
8,064 KB
testcase_18 AC 117 ms
7,936 KB
testcase_19 AC 91 ms
8,192 KB
testcase_20 AC 120 ms
8,064 KB
testcase_21 AC 125 ms
8,320 KB
testcase_22 AC 120 ms
8,064 KB
testcase_23 AC 118 ms
8,192 KB
testcase_24 AC 99 ms
7,936 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 3 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 3 ms
5,376 KB
testcase_38 AC 3 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 3 ms
5,376 KB
testcase_41 AC 3 ms
5,376 KB
testcase_42 AC 3 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 3 ms
5,376 KB
testcase_45 AC 125 ms
8,320 KB
testcase_46 AC 117 ms
7,940 KB
testcase_47 AC 116 ms
8,320 KB
testcase_48 AC 113 ms
8,064 KB
testcase_49 AC 111 ms
8,064 KB
testcase_50 AC 116 ms
8,064 KB
testcase_51 AC 130 ms
8,320 KB
testcase_52 AC 117 ms
7,936 KB
testcase_53 AC 118 ms
8,064 KB
testcase_54 AC 131 ms
8,192 KB
testcase_55 AC 126 ms
8,192 KB
testcase_56 AC 123 ms
8,064 KB
testcase_57 AC 118 ms
7,936 KB
testcase_58 AC 122 ms
8,192 KB
testcase_59 AC 126 ms
8,320 KB
testcase_60 AC 132 ms
8,320 KB
testcase_61 AC 121 ms
8,192 KB
testcase_62 AC 111 ms
7,944 KB
testcase_63 AC 127 ms
8,448 KB
testcase_64 AC 127 ms
8,320 KB
testcase_65 AC 11 ms
5,376 KB
testcase_66 AC 12 ms
5,376 KB
testcase_67 AC 52 ms
7,816 KB
testcase_68 AC 52 ms
8,064 KB
testcase_69 AC 62 ms
8,192 KB
testcase_70 AC 58 ms
8,448 KB
testcase_71 AC 34 ms
8,320 KB
testcase_72 AC 42 ms
7,808 KB
testcase_73 AC 37 ms
8,064 KB
testcase_74 AC 43 ms
8,192 KB
testcase_75 AC 60 ms
7,808 KB
testcase_76 AC 61 ms
8,064 KB
testcase_77 AC 55 ms
8,016 KB
testcase_78 AC 63 ms
8,192 KB
testcase_79 AC 79 ms
8,448 KB
testcase_80 AC 73 ms
8,064 KB
testcase_81 AC 75 ms
7,936 KB
testcase_82 AC 76 ms
8,192 KB
testcase_83 AC 44 ms
8,320 KB
testcase_84 AC 59 ms
8,192 KB
testcase_85 AC 23 ms
8,320 KB
testcase_86 AC 24 ms
7,936 KB
testcase_87 AC 68 ms
8,192 KB
testcase_88 AC 69 ms
7,956 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
権限があれば一括ダウンロードができます
コンパイルメッセージ
In member function 'void Treap<Val>::insert_b(Val) [with Val = long int]',
    inlined from 'int64_t solve(int, int, const std::vector<int>&)' at main.cpp:314:19:
main.cpp:229:27: warning: 'void operator delete(void*, std::size_t)' called on unallocated object 'pool' [-Wfree-nonheap-object]
  229 |     root = insert_b(root, new node(val));
      |                           ^~~~~~~~~~~~~
main.cpp: In function 'int64_t solve(int, int, const std::vector<int>&)':
main.cpp:21:19: note: declared here
   21 |       static Node pool[500010];
      |                   ^~~~

ソースコード

diff #

// O(N log K)

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

///////////////// ここから Treap

template <class Val>
struct Treap {
  struct Node {
    Val v;
    int priority;
    Node *lef, *rig;
    int size_;   // 部分木のサイズ
    Val summary; // 部分木の値の和

    Node() {}
    Node(Val v) : v(v), priority(xor128()), lef(nullptr), rig(nullptr), size_(1), summary(v) {}

    void *operator new(std::size_t) {
      static Node pool[500010];
      static int p = 0;
      return pool + p++;
    }

    int size() const { return size_;}
    string to_string() const { string res; to_string(res); return res; }

    void to_string(string &res) const {
      res += "Node [v="; res += std::to_string(v);
      res += ", size="; res += std::to_string(size_);
      res += "]";
    }

    static int xor128() {
      static random_device rnd;
      static int x = 123456789, y = 362436069, z = 521288629, w = rnd();
      int t = x ^ (x << 11);
      x = y;
      y = z;
      z = w;
      w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
      return w;
    }

    static void to_string(const Node* a, string &res, int indent) {
      if (a == nullptr) return;
      to_string(a->lef, res, indent + 2);
      for (int i = 0; i < indent; i++) res += ' ';
      a->to_string(res);
      res += '\n';
      to_string(a->rig, res, indent + 2);
    }

    static Node* update(Node* a) {
      if (a == nullptr) return nullptr;
      a->size_ = 1 + size(a->lef) + size(a->rig);

      a->summary = a->v;
      if (a->lef != nullptr) a->summary = a->summary + a->lef->summary;
      if (a->rig != nullptr) a->summary = a->summary + a->rig->summary;
      return a;
    }

    static int size(const Node* x) { return x == nullptr ? 0 : x->size_; }

    static vector<Node*> nodes(Node* a) {
      vector<Node*> ret(size(a));
      nodes(a, ret, 0, size(a));
      return ret;
    }

    static void nodes(Node* a, vector<Node*> &ns, int L, int R) {
      if (a == nullptr) return;
      nodes(a->lef, ns, L, L + size(a->lef));
      ns[L + size(a->lef)] = a;
      nodes(a->rig, ns, R - size(a->rig), R);
    }

    static Node* merge(Node* a, Node* b) {
      if (b == nullptr) return a;
      if (a == nullptr) return b;
      if (a->priority > b->priority) {
        a->rig = merge(a->rig, b);
        return update(a);
      }
      else {
        b->lef = merge(a, b->lef);
        return update(b);
      }
    }

    static pair<Node*, Node*> split(Node* a, int k) {
      if (a == nullptr) return make_pair(nullptr, nullptr);
      if (k <= size(a->lef)) {
        auto s = split(a->lef, k);
        a->lef = s.second;
        s.second = update(a);
        return s;
      }
      else {
        auto s = split(a->rig, k - size(a->lef) - 1);
        a->rig = s.first;
        s.first = update(a);
        return s;
      }
    }
  };

  using node = Node;
  using np = node*;

  static np merge_technically(np a, np b) {
    if (node::size(a) > node::size(b)) swap(a, b);
    // |a|<=|b|

    for (np cur : node::nodes(a)) {
      cur->lef = cur->rig = nullptr;
      b = insert_b(b, cur);
    }
    return b;
  }

  static pair<np, np> split_less(np a, Val v) {
    if (a == nullptr) return make_pair(nullptr, nullptr);
    if (a->v < v) {
      auto s = split_less(a->rig, v);
      a->rig = s.first;
      s.first = node::update(a);
      return s;
    }
    else {
      auto s = split_less(a->lef, v);
      a->lef = s.second;
      s.second = node::update(a);
      return s;
    }
  }

  static pair<np, np> split_leq(np a, Val v) {
    if (a == nullptr) return make_pair(nullptr, nullptr);
    if (a->v <= v) {
      auto s = split_leq(a->rig, v);
      a->rig = s.first;
      s.first = node::update(a);
      return s;
    }
    else {
      auto s = split_leq(a->lef, v);
      a->lef = s.second;
      s.second = node::update(a);
      return s;
    }
  }

  static np insert_k(np a, int k, const Val v) {
    auto lr = node::split(a, k);
    return node::merge(node::merge(lr.first, new node(v)), lr.second);
  }

  static np insert_b(np a, np v) {
    auto lr = split_less(a, v->v);
    return node::merge(node::merge(lr.first, v), lr.second);
  }

  static np erase_k(np a, int k) {
    auto lr = node::split(a, k);
    auto mr = node::split(lr.second, 1);
    return node::merge(lr.first, mr.second);
  }

  static np erase_b(np a, Val v) {
    auto lr = split_less(a, v);
    auto mr = node::split(lr.second, 1);
    return node::merge(lr.first, mr.second);
  }

  static np get_k(np a, int k) {
    while (a != nullptr) {
      if (k < node::size(a->lef)) {
        a = a->lef;
      }
      else if (k == node::size(a->lef)) {
        break;
      }
      else {
        k = k - node::size(a->lef) - 1;
        a = a->rig;
      }
    }
    return a;
  }

  public:
  np root;

  Treap() : root(nullptr) {}
  Treap(np root) : root(root) {}
  Treap(Treap l, Treap r) : root(node::merge(l.root, r.root)) {}

  string to_string() const { string res; node::to_string(root, res, 0); return res; }

  int size() const { return node::size(root); }

  // [0, k), [k, N) に分割する
  pair<Treap, Treap> split_k(int k) {
    auto lr = node::split(root, k);
    return make_pair(Treap(lr.first), Treap(lr.second));
  }

  // v を k 番目に入れる
  void insert_k(const Val val, int k) {
    root = insert_k(root, k, val);
  }

  // k番目を消す
  void erase_k(int k) {
    root = erase_k(root, k);
  }

  // k番目の値を取得する
  Val operator[](int k) const {
    assert(size() > k);
    return get_k(root, k)->v;
  }

  // v を Key の昇順位置に入れる
  void insert_b(const Val val) {
    root = insert_b(root, new node(val));
  }

  // 最初に見つけた val を消す
  void erase_b(const Val val) {
    if (contains_b(val)) root = erase_b(root, val);
  }

  // q 未満の要素の数を返す(q以上の要素の最小のindex)
  int count_less_b(const Val q) const {
    auto a = root;
    int lsize = 0;
    while (a != nullptr) {
      if (a->v < q) {
        lsize += size(a->lef) + 1;
        a = a->rig;
      }
      else {
        a = a->lef;
      }
    }
    return lsize;
  }

  // q 以上の要素の数を返す(qを越える要素の最小のindex)
  int count_leq_b(const Val q) const {
    auto a = root;
    int lsize = 0;
    while (a != nullptr) {
      if (a->v <= q) {
        lsize += size(a->lef) + 1;
        a = a->rig;
      }
      else {
        a = a->lef;
      }
    }
    return lsize;
  }

  // q の数を返す
  int count_b(const Val q) const {
    return count_leq(q) - count_less(q);
  }

  // q を含むかどうか
  bool contains_b(const Val q) const {
    auto a = root;
    while (a != nullptr) {
      if (a->v == q) return true;
      else if (a->v < q) a = a->rig;
      else a = a->lef;
    }
    return false;
  }

  // v未満の値 と v以上の値 に分割
  pair<Treap, Treap> split_less_b(const Val v) {
    auto lr = split_less(root, v);
    return make_pair(Treap(lr.first), Treap(lr.second));
  }

  // v以下の値 と vより大きい値 に分割
  pair<Treap, Treap> split_leq_b(const Val v) {
    auto lr = split_leq(root, v);
    return make_pair(Treap(lr.first), Treap(lr.second));
  }
};
template<class Val> ostream& operator<<(ostream& os, const Treap<Val>& treap) { os << treap.to_string(); return os; }

///////////////// ここまで Treap

///////////////// ここから 解答

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

  int l = (k + 1) / 2;
  int m = k - l;
  Treap<int64_t> treap;

  int64_t ans = 1e18;

  for (int i = 0; i < n; i++) {
    // 値を入れる O(log K)
    treap.insert_b(A[i]);

    // 範囲外に出たものを取り除く O(log K)
    if (i >= k) treap.erase_b(A[i-k]);

    if (i >= k - 1) {
      // treap に A[i-k+1] から A[i] までの k 個の値が入っているので、答えの候補を求める

      // 中央値を求める O(log K)
      int64_t median = treap[l-1];

      // 以下で中央値に揃えるコストを求める

      // [0, l), [l, k) で split O(log K)
      auto splited = treap.split_k(l);

      // 小さい l 個の和 O(1)
      int64_t sum_of_lower = splited.first.root->summary;

      // 大きい m 個の和 O(1)
      int64_t sum_of_higher = splited.second.root->summary;

      // merge O(log K)
      treap = Treap<int64_t>(splited.first, splited.second);

      // コスト計算
      int64_t inc_cost = median * l - sum_of_lower;
      int64_t dec_cost = sum_of_higher - median * m;
      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