結果

問題 No.738 平らな農地
ユーザー しらっ亭しらっ亭
提出日時 2017-06-25 20:25:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 3,852 bytes
コンパイル時間 2,289 ms
コンパイル使用メモリ 183,872 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2024-04-21 14:56:15
合計ジャッジ時間 9,736 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 4 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 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 119 ms
9,360 KB
testcase_16 AC 122 ms
9,216 KB
testcase_17 AC 127 ms
9,344 KB
testcase_18 AC 120 ms
9,216 KB
testcase_19 AC 98 ms
9,792 KB
testcase_20 AC 125 ms
9,472 KB
testcase_21 AC 131 ms
9,728 KB
testcase_22 AC 125 ms
9,344 KB
testcase_23 AC 126 ms
9,492 KB
testcase_24 AC 111 ms
9,600 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 3 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 132 ms
9,984 KB
testcase_46 AC 126 ms
9,228 KB
testcase_47 AC 127 ms
9,728 KB
testcase_48 AC 118 ms
9,392 KB
testcase_49 AC 115 ms
9,344 KB
testcase_50 AC 123 ms
9,600 KB
testcase_51 AC 129 ms
9,752 KB
testcase_52 AC 117 ms
9,344 KB
testcase_53 AC 128 ms
9,472 KB
testcase_54 AC 134 ms
9,728 KB
testcase_55 AC 137 ms
9,728 KB
testcase_56 AC 132 ms
9,600 KB
testcase_57 AC 118 ms
9,216 KB
testcase_58 AC 124 ms
9,600 KB
testcase_59 AC 126 ms
9,984 KB
testcase_60 AC 126 ms
9,728 KB
testcase_61 AC 127 ms
9,728 KB
testcase_62 AC 118 ms
9,360 KB
testcase_63 AC 132 ms
9,856 KB
testcase_64 AC 136 ms
9,668 KB
testcase_65 AC 10 ms
5,376 KB
testcase_66 AC 10 ms
5,376 KB
testcase_67 AC 62 ms
9,216 KB
testcase_68 AC 62 ms
9,600 KB
testcase_69 AC 70 ms
9,644 KB
testcase_70 AC 70 ms
9,856 KB
testcase_71 AC 45 ms
9,728 KB
testcase_72 AC 47 ms
9,092 KB
testcase_73 AC 50 ms
9,472 KB
testcase_74 AC 58 ms
9,472 KB
testcase_75 AC 71 ms
9,088 KB
testcase_76 AC 68 ms
9,344 KB
testcase_77 AC 67 ms
9,088 KB
testcase_78 AC 74 ms
9,728 KB
testcase_79 AC 90 ms
9,728 KB
testcase_80 AC 81 ms
9,600 KB
testcase_81 AC 88 ms
9,472 KB
testcase_82 AC 88 ms
9,728 KB
testcase_83 AC 65 ms
9,856 KB
testcase_84 AC 67 ms
9,728 KB
testcase_85 AC 37 ms
9,728 KB
testcase_86 AC 37 ms
9,344 KB
testcase_87 AC 81 ms
9,472 KB
testcase_88 AC 76 ms
9,332 KB
testcase_89 AC 2 ms
5,376 KB
testcase_90 AC 1 ms
5,376 KB
testcase_91 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

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++) {
		treap.insert_b(A[i]);

		if (i >= k) treap.erase_b(A[i-k]);

		if (i >= k - 1) {
			int64_t median = treap[l-1];
			auto splited = treap.split_k(l);
			int64_t sum_of_lower = splited.first.root->sum;
			int64_t sum_of_higher = splited.second.root->sum;
			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