結果

問題 No.738 平らな農地
ユーザー ats5515ats5515
提出日時 2018-09-28 22:47:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 4,101 bytes
コンパイル時間 1,081 ms
コンパイル使用メモリ 94,136 KB
実行使用メモリ 10,240 KB
最終ジャッジ日時 2024-04-20 11:48:07
合計ジャッジ時間 8,038 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 91 ms
9,472 KB
testcase_16 AC 94 ms
9,600 KB
testcase_17 AC 93 ms
9,600 KB
testcase_18 AC 91 ms
9,472 KB
testcase_19 AC 66 ms
9,984 KB
testcase_20 AC 94 ms
9,600 KB
testcase_21 AC 96 ms
9,984 KB
testcase_22 AC 95 ms
9,728 KB
testcase_23 AC 91 ms
9,984 KB
testcase_24 AC 79 ms
9,728 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 2 ms
6,944 KB
testcase_45 AC 92 ms
10,112 KB
testcase_46 AC 87 ms
9,600 KB
testcase_47 AC 91 ms
9,856 KB
testcase_48 AC 83 ms
9,600 KB
testcase_49 AC 81 ms
9,600 KB
testcase_50 AC 85 ms
9,984 KB
testcase_51 AC 95 ms
9,984 KB
testcase_52 AC 83 ms
9,728 KB
testcase_53 AC 89 ms
9,856 KB
testcase_54 AC 94 ms
9,984 KB
testcase_55 AC 93 ms
9,984 KB
testcase_56 AC 95 ms
9,856 KB
testcase_57 AC 85 ms
9,472 KB
testcase_58 AC 91 ms
9,856 KB
testcase_59 AC 94 ms
10,240 KB
testcase_60 AC 94 ms
10,112 KB
testcase_61 AC 88 ms
9,984 KB
testcase_62 AC 86 ms
9,600 KB
testcase_63 AC 100 ms
10,112 KB
testcase_64 AC 94 ms
9,984 KB
testcase_65 AC 18 ms
9,600 KB
testcase_66 AC 19 ms
9,856 KB
testcase_67 AC 51 ms
9,472 KB
testcase_68 AC 52 ms
9,728 KB
testcase_69 AC 55 ms
9,856 KB
testcase_70 AC 59 ms
10,112 KB
testcase_71 AC 133 ms
9,984 KB
testcase_72 AC 136 ms
9,472 KB
testcase_73 AC 126 ms
9,600 KB
testcase_74 AC 141 ms
9,856 KB
testcase_75 AC 51 ms
9,600 KB
testcase_76 AC 55 ms
9,856 KB
testcase_77 AC 45 ms
9,600 KB
testcase_78 AC 47 ms
9,984 KB
testcase_79 AC 57 ms
10,240 KB
testcase_80 AC 57 ms
9,728 KB
testcase_81 AC 57 ms
9,728 KB
testcase_82 AC 47 ms
9,856 KB
testcase_83 AC 143 ms
10,112 KB
testcase_84 AC 160 ms
9,856 KB
testcase_85 AC 22 ms
9,984 KB
testcase_86 AC 22 ms
9,600 KB
testcase_87 AC 52 ms
9,728 KB
testcase_88 AC 47 ms
9,472 KB
testcase_89 AC 1 ms
6,940 KB
testcase_90 AC 2 ms
6,940 KB
testcase_91 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function ‘std::pair<AVL<T>::node*, long long int> AVL<T>::rank(AVL<T>::node*, long long int) [with T = long long int]’:
main.cpp:118:9: warning: control reaches end of non-void function [-Wreturn-type]
  118 |         }
      |         ^

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
template<typename T>
struct AVL {
	struct node {
		T key;
		int size, height, sum;
		node *child[2];
		node(const T &key) :key(key), size(1), height(1),sum(key) {
			child[0] = child[1] = 0;
		}
	} *root;
	typedef node *pointer;
	AVL() { root = NULL; }
	pointer find(T key) {
		return find(root, key);
	}
	node *find(node *t, const T key) {
		if (t == NULL) return NULL;
		if (key == (t->key)) return t;
		else if (key < (t->key)) return find(t->child[0], key);
		else return find(t->child[1], key);
	}
	void insert(const T key) {
		root = insert(root, new node(key));
	}
	node *insert(node *t, node *x) {
		if (t == NULL) return x;
		if ((x->key) <= (t->key)) t->child[0] = insert(t->child[0], x);
		else t->child[1] = insert(t->child[1], x);
		t->size += 1;
		return balance(t);
	}
	void erase(const T key) {
		T t = key;
		if (find(t) == NULL) return;
		root = erase(root, key);
	}
	node *erase(node *t, const int x) {
		if (t == NULL) return NULL;
		if (x == (t->key)) {
			return move_down(t->child[0], t->child[1]);
		}
		else {
			if (x < (t->key)) t->child[0] = erase(t->child[0], x);
			else t->child[1] = erase(t->child[1], x);
			t->size -= 1;
			return balance(t);
		}
	}
	node *move_down(node *t, node *rhs) {
		if (t == NULL) return rhs;
		t->child[1] = move_down(t->child[1], rhs);
		return balance(t);
	}
	int sz(node *t) {
		if (t != NULL) return t->size;
		return 0;
	}
	int ht(node *t) {
		if (t != NULL) return t->height;
		return 0;
	}
	int sm(node *t) {
		if (t != NULL) return t->sum;
		return 0;
	}
	node *rotate(node *t, int l, int r) {
		node *s = t->child[r];
		t->child[r] = s->child[l];
		s->child[l] = balance(t);
		if (t != NULL) t->size = sz(t->child[0]) + sz(t->child[1]) + 1;
		if (s != NULL) s->size = sz(s->child[0]) + sz(s->child[1]) + 1;
		if (t != NULL) t->sum = sm(t->child[0]) + sm(t->child[1]) + t->key;
		if (s != NULL) s->sum = sm(s->child[0]) + sm(s->child[1]) + s->key;
		return balance(s);
	}
	node *balance(node *t) {
		for (int i = 0; i < 2; i++) {
			if (ht(t->child[!i]) - ht(t->child[i]) < -1) {
				if (ht(t->child[i]->child[!i]) - ht(t->child[i]->child[i]) > 0)
					t->child[i] = rotate(t->child[i], i, !i);
				return rotate(t, !i, i);
			}
		}
		if (t != NULL) t->height = max(ht(t->child[0]), ht(t->child[1])) + 1;
		if (t != NULL) t->size = sz(t->child[0]) + sz(t->child[1]) + 1;
		if (t != NULL) t->sum = sm(t->child[0]) + sm(t->child[1]) + t->key;
		return t;
	}
	pair<pointer, int> rank(int k) {
		return rank(root, k);
	}
	pair<pointer, int> rank(node *t, int k) {
		if (t == NULL) return make_pair((pointer)NULL, (int)0);
		int m = sz(t->child[0]);
		if (k < m) {
			pair<pointer, int> res = rank(t->child[0], k);
			return res;
		}
		if (k == m) return make_pair(t, t->key + sm(t->child[0]));
		if (k > m) {
			pair<pointer, int> res = rank(t->child[1], k - m - 1);
			res.second += t->key + sm(t->child[0]);
			return res;
		}
	}
	int index(T key) {
		if (find(key) == NULL) return -1;
		return index(root, key);
	}
	int index(node *t, T key) {
		if (key == (t->key)) return sz(t->child[0]);
		if (key < (t->key)) return index(t->child[0], key);
		else return sz(t) - sz(t->child[1]) + index(t->child[1], key);
	}
};
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, K;
	cin >> N >> K;
	vector<int> A(N);
	int res = (int)1 << 60;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	AVL<int> avl;
	int sum = 0;
	for (int i = 0; i < K - 1; i++) {
		avl.insert(A[i]);
		sum += A[i];
	}
	int ans;
	int l = ((K + 1) / 2);
	for (int i = K - 1; i < N; i++) {
		avl.insert(A[i]);
		sum += A[i];

		auto p = avl.rank((K - 1) / 2);
		ans = p.first->key * l - p.second;
		ans += (sum - p.second) - p.first->key * (K - l);
		res = min(res, ans);
		//cerr << i << " " << p.first->key << " " << p.second << endl;
		avl.erase(A[i - K + 1]);
		sum -= A[i - K + 1];
	}

	cout << res << endl;
}
0