結果

問題 No.738 平らな農地
ユーザー chocoruskchocorusk
提出日時 2018-10-14 09:40:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 3,004 bytes
コンパイル時間 1,211 ms
コンパイル使用メモリ 96,644 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2024-04-20 20:15:22
合計ジャッジ時間 11,601 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 6 ms
5,376 KB
testcase_07 AC 6 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 160 ms
9,856 KB
testcase_16 AC 162 ms
9,984 KB
testcase_17 AC 183 ms
9,856 KB
testcase_18 AC 166 ms
9,728 KB
testcase_19 AC 127 ms
10,368 KB
testcase_20 AC 174 ms
9,984 KB
testcase_21 AC 171 ms
10,240 KB
testcase_22 AC 166 ms
9,984 KB
testcase_23 AC 160 ms
10,240 KB
testcase_24 AC 146 ms
9,984 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 4 ms
5,376 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 3 ms
5,376 KB
testcase_34 AC 4 ms
5,376 KB
testcase_35 AC 3 ms
5,376 KB
testcase_36 AC 4 ms
5,376 KB
testcase_37 AC 3 ms
5,376 KB
testcase_38 AC 3 ms
5,376 KB
testcase_39 AC 3 ms
5,376 KB
testcase_40 AC 4 ms
5,376 KB
testcase_41 AC 3 ms
5,376 KB
testcase_42 AC 3 ms
5,376 KB
testcase_43 AC 3 ms
5,376 KB
testcase_44 AC 4 ms
5,376 KB
testcase_45 AC 172 ms
10,368 KB
testcase_46 AC 157 ms
9,984 KB
testcase_47 AC 165 ms
10,240 KB
testcase_48 AC 155 ms
9,984 KB
testcase_49 AC 158 ms
9,984 KB
testcase_50 AC 156 ms
10,112 KB
testcase_51 AC 172 ms
10,240 KB
testcase_52 AC 157 ms
9,984 KB
testcase_53 AC 162 ms
10,112 KB
testcase_54 AC 170 ms
10,368 KB
testcase_55 AC 177 ms
10,240 KB
testcase_56 AC 174 ms
10,112 KB
testcase_57 AC 161 ms
9,856 KB
testcase_58 AC 163 ms
10,240 KB
testcase_59 AC 168 ms
10,496 KB
testcase_60 AC 168 ms
10,368 KB
testcase_61 AC 169 ms
10,240 KB
testcase_62 AC 156 ms
9,984 KB
testcase_63 AC 179 ms
10,496 KB
testcase_64 AC 170 ms
10,240 KB
testcase_65 AC 42 ms
9,856 KB
testcase_66 AC 45 ms
10,240 KB
testcase_67 AC 89 ms
9,856 KB
testcase_68 AC 90 ms
10,112 KB
testcase_69 AC 105 ms
10,240 KB
testcase_70 AC 99 ms
10,496 KB
testcase_71 AC 73 ms
10,368 KB
testcase_72 AC 104 ms
9,984 KB
testcase_73 AC 122 ms
10,112 KB
testcase_74 AC 98 ms
10,112 KB
testcase_75 AC 99 ms
9,856 KB
testcase_76 AC 111 ms
9,984 KB
testcase_77 AC 98 ms
9,728 KB
testcase_78 AC 105 ms
10,240 KB
testcase_79 AC 126 ms
10,496 KB
testcase_80 AC 113 ms
10,240 KB
testcase_81 AC 119 ms
9,856 KB
testcase_82 AC 118 ms
10,112 KB
testcase_83 AC 119 ms
10,496 KB
testcase_84 AC 116 ms
10,112 KB
testcase_85 AC 55 ms
10,368 KB
testcase_86 AC 53 ms
9,856 KB
testcase_87 AC 106 ms
10,112 KB
testcase_88 AC 102 ms
9,856 KB
testcase_89 AC 3 ms
5,376 KB
testcase_90 AC 3 ms
5,376 KB
testcase_91 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

random_device rnd;
mt19937 mt(rnd());
uniform_real_distribution<> rnd1(0, 1.0);

struct node_t{
	ll val;
	ll sum;
	node_t *lch;
	node_t *rch;
	double pri;
	int cnt;
	node_t() {}
	node_t(ll v, double p):val(v), pri(p), cnt(1), sum(v), lch(nullptr), rch(nullptr){}

};

int count(node_t *t){
	return !t ? 0 : t->cnt;
}

ll sum(node_t *t){
	return !t ? 0 : t->sum;
}

node_t *update(node_t *t){
	t->cnt=count(t->lch)+count(t->rch)+1;
	t->sum=sum(t->lch)+sum(t->rch)+(t->val);
	return t;
}

node_t *merge(node_t *l, node_t *r){
	if(!l || !r) return !l ? r : l;
	if(l->pri > r->pri){
		l->rch=merge(l->rch, r);
		return update(l);
	}else{
		r->lch=merge(l, r->lch);
		return update(r);
	}
}

pair<node_t*, node_t*> split(node_t *t, ll x){
	if(!t) return make_pair(nullptr, nullptr);
	if(x <= t->val){
		pair<node_t*, node_t*> s=split(t->lch, x);
		t->lch=s.second;
		return make_pair(s.first, update(t));
	}else{
		pair<node_t*, node_t*> s=split(t->rch, x);
		t->rch=s.first;
		return make_pair(update(t), s.second);
	}
}

pair<node_t*, node_t*> split2(node_t *t, int k){ //[0, k), [k,n)
	if(!t) return make_pair(nullptr, nullptr);
	if(k <= count(t->lch)){
		pair<node_t*, node_t*> s=split2(t->lch, k);
		t->lch=s.second;
		return make_pair(s.first, update(t));
	}else{
		pair<node_t*, node_t*> s=split2(t->rch, k-1-count(t->lch));
		t->rch=s.first;
		return make_pair(update(t), s.second);
	}
}

node_t *insert(node_t *t, ll x){
	pair<node_t*, node_t*> s=split(t, x);
	return merge(merge(s.first, new node_t(x, rnd1(mt))), s.second);
}

node_t *erase(node_t *t, int k){
	pair<node_t*, node_t*> s=split2(t, k);
	pair<node_t*, node_t*> s2=split2(s.second, 1);
	return merge(s.first, s2.second);
}

ll find(node_t *t, int k){
	if(count(t->lch)==k){
		return t->val;
	}else if(count(t->lch)<k){
		return find(t->rch, k-count(t->lch)-1);
	}else{
		return find(t->lch, k);
	}
}

void find2(node_t *t, ll x, int& ans){
	if(x==(t->val)){
		ans+=count(t->lch);
		return;
	}else if(x<(t->val)){
		return find2(t->lch, x, ans);
	}else{
		ans+=(count(t->lch)+1);
		return find2(t->rch, x, ans);
	}
}

ll calc(node_t *t, int k){
	ll x=find(t, k);
	pair<node_t*, node_t*> s=split2(t, k);
	ll s1=sum(s.first), s2=sum(s.second);
	ll ans=x*(ll)k-s1+s2-x*(ll)(count(s.second));
	t=merge(s.first, s.second);
	return ans;
}

int main()
{
	int n, k; cin>>n>>k;
	ll a[100000];
	for(int i=0; i<n; i++){
		cin>>a[i];
	}
	node_t *s=nullptr;
	for(int i=0; i<k; i++){
		s=insert(s, a[i]);
	}
	ll ans=calc(s, k/2);
	for(int i=k; i<n; i++){
		s=insert(s, a[i]);
		int ct=0;
		find2(s, a[i-k], ct);
		s=erase(s, ct);
		ans=min(ans, calc(s, k/2));
	}
	cout<<ans<<endl;
    return 0;
}
0