結果

問題 No.738 平らな農地
ユーザー penguinshunyapenguinshunya
提出日時 2018-10-29 15:41:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 4,669 bytes
コンパイル時間 3,037 ms
コンパイル使用メモリ 220,260 KB
実行使用メモリ 9,952 KB
最終ジャッジ日時 2023-08-12 08:21:41
合計ジャッジ時間 13,188 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 136 ms
9,280 KB
testcase_16 AC 144 ms
9,156 KB
testcase_17 AC 137 ms
9,236 KB
testcase_18 AC 134 ms
9,144 KB
testcase_19 AC 107 ms
9,736 KB
testcase_20 AC 142 ms
9,164 KB
testcase_21 AC 146 ms
9,668 KB
testcase_22 AC 143 ms
9,184 KB
testcase_23 AC 137 ms
9,464 KB
testcase_24 AC 125 ms
9,252 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 3 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 3 ms
4,376 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 150 ms
9,672 KB
testcase_46 AC 138 ms
9,212 KB
testcase_47 AC 148 ms
9,432 KB
testcase_48 AC 132 ms
9,348 KB
testcase_49 AC 136 ms
9,136 KB
testcase_50 AC 137 ms
9,448 KB
testcase_51 AC 148 ms
9,496 KB
testcase_52 AC 135 ms
9,176 KB
testcase_53 AC 146 ms
9,360 KB
testcase_54 AC 147 ms
9,620 KB
testcase_55 AC 158 ms
9,452 KB
testcase_56 AC 152 ms
9,440 KB
testcase_57 AC 140 ms
9,156 KB
testcase_58 AC 149 ms
9,396 KB
testcase_59 AC 159 ms
9,652 KB
testcase_60 AC 147 ms
9,664 KB
testcase_61 AC 150 ms
9,656 KB
testcase_62 AC 144 ms
9,092 KB
testcase_63 AC 155 ms
9,952 KB
testcase_64 AC 156 ms
9,672 KB
testcase_65 AC 10 ms
4,380 KB
testcase_66 AC 11 ms
4,376 KB
testcase_67 AC 66 ms
9,112 KB
testcase_68 AC 66 ms
9,388 KB
testcase_69 AC 76 ms
9,472 KB
testcase_70 AC 72 ms
9,676 KB
testcase_71 AC 41 ms
9,748 KB
testcase_72 AC 52 ms
9,160 KB
testcase_73 AC 52 ms
9,236 KB
testcase_74 AC 46 ms
9,384 KB
testcase_75 AC 77 ms
9,076 KB
testcase_76 AC 77 ms
9,580 KB
testcase_77 AC 73 ms
9,320 KB
testcase_78 AC 79 ms
9,756 KB
testcase_79 AC 99 ms
9,940 KB
testcase_80 AC 89 ms
9,404 KB
testcase_81 AC 91 ms
9,332 KB
testcase_82 AC 93 ms
9,412 KB
testcase_83 AC 68 ms
9,680 KB
testcase_84 AC 82 ms
9,492 KB
testcase_85 AC 39 ms
9,740 KB
testcase_86 AC 39 ms
9,208 KB
testcase_87 AC 99 ms
9,412 KB
testcase_88 AC 96 ms
9,100 KB
testcase_89 AC 1 ms
4,380 KB
testcase_90 AC 2 ms
4,380 KB
testcase_91 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;
using i32 = int;
using i64 = long long;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vvi32 = vector<vi32>;
using vi64 = vector<i64>;
using vvi64 = vector<vi64>;
using vstr = vector<string>;

template <typename T> inline bool amax(T &x, T y) { if (x < y) { x = y; return true; } return false; }
template <typename T> inline bool amin(T &x, T y) { if (y < x) { x = y; return true; } return false; }

random_device rng;

template <class Val>
struct Treap {
  struct Node {
    Val v;
    int priority;
    Node *lef, *rig;
    int size_;
    Val sum;
    
    Node(Val v) : v(v), priority(rng()), lef(nullptr), rig(nullptr), size_(1), sum(v) {}
    
    int size() const { return size_; }

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

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

    static Node* merge(Node* a, Node* b) {
      if (a == nullptr) return b;
      if (b == nullptr) return a;
      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 np = Node*;

  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 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_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)) {}

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

  pair<Treap, Treap> split_k(int k) {
    auto lr = Node::split(root, k);
    return make_pair(Treap(lr.first), Treap(lr.second));
  }

  Val operator[](int k) const {
    assert(size() > k);
    return get_k(root, k)->v;
  }

  void insert_b(const Val val) {
    root = insert_b(root, new Node(val));
  }

  void erase_b(const Val val) {
    if (contains_b(val)) root = erase_b(root, val);
  }

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

i64 solve(const int n, const int k, const vi32 &A) {
  if (k == 1) return 0;

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

  i64 ans = 1e18;

  rep(i, n) {
    treap.insert_b(A[i]);
    if (i >= k) treap.erase_b(A[i - k]);
    if (i >= k - 1) {
      i64 mid = treap[l - 1];
      
      auto splited = treap.split_k(l);
      
      i64 sum_of_lower = splited.first.root->sum;
      i64 sum_of_higher = splited.second.root->sum;
      
      treap = Treap<i64>(splited.first, splited.second);

      i64 inc_cost = mid * l - sum_of_lower;
      i64 dec_cost = sum_of_higher - mid * m;
      i64 cand = inc_cost + dec_cost;

      amin(ans, cand);
    }
  }

  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(16);
  int n, k;
  cin >> n >> k;
  vi32 A(n);
  rep(i, n) cin >> A[i];
  cout << solve(n, k, A) << endl;
  return 0;
}
0