結果
問題 | No.738 平らな農地 |
ユーザー | penguinshunya |
提出日時 | 2018-10-29 15:41:00 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 165 ms / 2,000 ms |
コード長 | 4,669 bytes |
コンパイル時間 | 2,923 ms |
コンパイル使用メモリ | 222,816 KB |
実行使用メモリ | 9,908 KB |
最終ジャッジ日時 | 2024-11-19 07:40:09 |
合計ジャッジ時間 | 11,899 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 2 ms
6,820 KB |
testcase_04 | AC | 2 ms
6,816 KB |
testcase_05 | AC | 4 ms
6,820 KB |
testcase_06 | AC | 5 ms
6,816 KB |
testcase_07 | AC | 5 ms
6,820 KB |
testcase_08 | AC | 3 ms
6,820 KB |
testcase_09 | AC | 3 ms
6,820 KB |
testcase_10 | AC | 2 ms
6,820 KB |
testcase_11 | AC | 3 ms
6,816 KB |
testcase_12 | AC | 2 ms
6,816 KB |
testcase_13 | AC | 4 ms
6,820 KB |
testcase_14 | AC | 3 ms
6,820 KB |
testcase_15 | AC | 144 ms
9,236 KB |
testcase_16 | AC | 147 ms
9,344 KB |
testcase_17 | AC | 142 ms
9,344 KB |
testcase_18 | AC | 144 ms
9,216 KB |
testcase_19 | AC | 110 ms
9,728 KB |
testcase_20 | AC | 144 ms
9,472 KB |
testcase_21 | AC | 149 ms
9,728 KB |
testcase_22 | AC | 150 ms
9,472 KB |
testcase_23 | AC | 140 ms
9,600 KB |
testcase_24 | AC | 127 ms
9,472 KB |
testcase_25 | AC | 3 ms
6,820 KB |
testcase_26 | AC | 3 ms
6,820 KB |
testcase_27 | AC | 3 ms
6,816 KB |
testcase_28 | AC | 3 ms
6,820 KB |
testcase_29 | AC | 3 ms
6,820 KB |
testcase_30 | AC | 3 ms
6,816 KB |
testcase_31 | AC | 3 ms
6,820 KB |
testcase_32 | AC | 3 ms
6,820 KB |
testcase_33 | AC | 3 ms
6,816 KB |
testcase_34 | AC | 3 ms
6,816 KB |
testcase_35 | AC | 3 ms
6,820 KB |
testcase_36 | AC | 2 ms
6,816 KB |
testcase_37 | AC | 3 ms
6,820 KB |
testcase_38 | AC | 3 ms
6,820 KB |
testcase_39 | AC | 3 ms
6,820 KB |
testcase_40 | AC | 3 ms
6,820 KB |
testcase_41 | AC | 2 ms
6,816 KB |
testcase_42 | AC | 3 ms
6,816 KB |
testcase_43 | AC | 3 ms
6,820 KB |
testcase_44 | AC | 3 ms
6,820 KB |
testcase_45 | AC | 164 ms
9,856 KB |
testcase_46 | AC | 142 ms
9,216 KB |
testcase_47 | AC | 149 ms
9,600 KB |
testcase_48 | AC | 136 ms
9,344 KB |
testcase_49 | AC | 138 ms
9,344 KB |
testcase_50 | AC | 136 ms
9,728 KB |
testcase_51 | AC | 158 ms
9,728 KB |
testcase_52 | AC | 140 ms
9,344 KB |
testcase_53 | AC | 152 ms
9,472 KB |
testcase_54 | AC | 154 ms
9,728 KB |
testcase_55 | AC | 158 ms
9,728 KB |
testcase_56 | AC | 150 ms
9,472 KB |
testcase_57 | AC | 142 ms
9,216 KB |
testcase_58 | AC | 158 ms
9,600 KB |
testcase_59 | AC | 163 ms
9,856 KB |
testcase_60 | AC | 158 ms
9,728 KB |
testcase_61 | AC | 156 ms
9,908 KB |
testcase_62 | AC | 143 ms
9,236 KB |
testcase_63 | AC | 165 ms
9,856 KB |
testcase_64 | AC | 149 ms
9,728 KB |
testcase_65 | AC | 11 ms
6,816 KB |
testcase_66 | AC | 11 ms
6,820 KB |
testcase_67 | AC | 68 ms
9,240 KB |
testcase_68 | AC | 68 ms
9,472 KB |
testcase_69 | AC | 79 ms
9,600 KB |
testcase_70 | AC | 75 ms
9,856 KB |
testcase_71 | AC | 59 ms
9,728 KB |
testcase_72 | AC | 60 ms
9,220 KB |
testcase_73 | AC | 52 ms
9,472 KB |
testcase_74 | AC | 53 ms
9,600 KB |
testcase_75 | AC | 78 ms
9,216 KB |
testcase_76 | AC | 77 ms
9,472 KB |
testcase_77 | AC | 74 ms
9,216 KB |
testcase_78 | AC | 81 ms
9,856 KB |
testcase_79 | AC | 100 ms
9,856 KB |
testcase_80 | AC | 90 ms
9,600 KB |
testcase_81 | AC | 94 ms
9,344 KB |
testcase_82 | AC | 95 ms
9,600 KB |
testcase_83 | AC | 66 ms
9,856 KB |
testcase_84 | AC | 72 ms
9,472 KB |
testcase_85 | AC | 40 ms
9,600 KB |
testcase_86 | AC | 41 ms
9,088 KB |
testcase_87 | AC | 104 ms
9,600 KB |
testcase_88 | AC | 97 ms
9,376 KB |
testcase_89 | AC | 2 ms
6,820 KB |
testcase_90 | AC | 2 ms
6,816 KB |
testcase_91 | AC | 2 ms
6,820 KB |
ソースコード
#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; }