結果

問題 No.738 平らな農地
ユーザー azbrugbyazbrugby
提出日時 2019-02-23 18:27:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,030 bytes
コンパイル時間 1,613 ms
コンパイル使用メモリ 78,060 KB
実行使用メモリ 8,772 KB
最終ジャッジ日時 2023-08-20 18:19:09
合計ジャッジ時間 11,516 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 83 ms
5,732 KB
testcase_16 AC 83 ms
5,684 KB
testcase_17 AC 75 ms
4,720 KB
testcase_18 AC 78 ms
4,832 KB
testcase_19 AC 60 ms
4,384 KB
testcase_20 AC 83 ms
5,816 KB
testcase_21 AC 80 ms
4,640 KB
testcase_22 AC 83 ms
5,444 KB
testcase_23 AC 76 ms
4,480 KB
testcase_24 AC 67 ms
4,384 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,384 KB
testcase_43 AC 3 ms
4,376 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 95 ms
6,436 KB
testcase_46 AC 84 ms
6,020 KB
testcase_47 AC 92 ms
6,268 KB
testcase_48 AC 88 ms
6,552 KB
testcase_49 AC 89 ms
6,520 KB
testcase_50 AC 96 ms
6,776 KB
testcase_51 AC 93 ms
6,092 KB
testcase_52 AC 85 ms
6,076 KB
testcase_53 AC 89 ms
6,080 KB
testcase_54 AC 93 ms
6,096 KB
testcase_55 AC 94 ms
6,188 KB
testcase_56 AC 89 ms
6,020 KB
testcase_57 AC 82 ms
6,220 KB
testcase_58 AC 91 ms
6,268 KB
testcase_59 AC 95 ms
6,672 KB
testcase_60 AC 96 ms
6,704 KB
testcase_61 AC 93 ms
6,584 KB
testcase_62 AC 86 ms
6,396 KB
testcase_63 AC 94 ms
6,088 KB
testcase_64 AC 94 ms
6,304 KB
testcase_65 AC 37 ms
4,380 KB
testcase_66 AC 38 ms
4,388 KB
testcase_67 AC 83 ms
6,056 KB
testcase_68 AC 87 ms
6,232 KB
testcase_69 AC 63 ms
4,604 KB
testcase_70 AC 76 ms
5,848 KB
testcase_71 AC 41 ms
4,524 KB
testcase_72 AC 68 ms
4,884 KB
testcase_73 AC 69 ms
5,808 KB
testcase_74 AC 63 ms
4,652 KB
testcase_75 AC 65 ms
4,640 KB
testcase_76 AC 76 ms
5,788 KB
testcase_77 AC 56 ms
4,380 KB
testcase_78 AC 61 ms
4,448 KB
testcase_79 AC 68 ms
4,476 KB
testcase_80 AC 76 ms
5,600 KB
testcase_81 AC 60 ms
4,652 KB
testcase_82 AC 54 ms
4,380 KB
testcase_83 AC 62 ms
5,032 KB
testcase_84 AC 70 ms
5,064 KB
testcase_85 AC 45 ms
4,380 KB
testcase_86 AC 44 ms
4,380 KB
testcase_87 AC 83 ms
8,772 KB
testcase_88 AC 78 ms
8,292 KB
testcase_89 AC 2 ms
4,376 KB
testcase_90 AC 2 ms
4,376 KB
testcase_91 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <cstdio>
#include <cmath>
#define mk make_pair
#define pb push_back
#define printf printf_s
#define scanf scanf_s
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pos;
const ll MOD = 1000000007, N = 200010, dx[4] = { -1,1,0,0 }, dy[4] = { 0,0,-1,1 }, MIN = -72340172838076673, MAX = 72340172838076673;

ll mn(ll a, ll b) {
	if (a == -1)return b;
	if (b == -1)return a;
	return min(a, b);
}
ll gcd(ll v1, ll v2) {
	if (v1 == 0)return v2; if (v2 == 0)return v1; if (v2 > v1)return gcd(v2%v1, v1); return gcd(v1%v2, v2);
}

ll pw(ll v1, ll v2) {
	ll v3 = 1;
	while (v2 > 0) {
		if (v2 % 2)v3 = (v3*v1) % MOD;
		v1 = (v1*v1) % MOD;
		v2 /= 2;
	}
	return v3;
}

struct ab {
	ll a, b;
	bool operator<(const ab& right) const {
		return right.b*a - right.a*b < 0;
	}
};

ll n, k,a[N],sv1=0,sv2=0;
multiset<ll> st1, st2;
void ev() {
	while (st1.size() > st2.size() + 1) {
		ll v1 = *st1.begin();
		sv1 -= v1;
		st2.insert(-v1);
		sv2 += -v1;
		st1.erase(st1.begin());
	}
	while (st1.size() < st2.size()) {
		ll v1 = *st2.begin();
		sv1 += -v1;
		sv2 -= v1;
		st2.erase(st2.begin());
		st1.insert(-v1);
	}
	while(!st2.empty()&&-*st1.begin()>*st2.begin()) {
		ll  v1 = *st1.begin(),v2=*st2.begin();
		sv1 += -v1 - v2;
		sv2 += -v1 - v2;
		st1.erase(st1.begin());
		st2.erase(st2.begin());
		st1.insert(-v2);
		st2.insert(-v1);
	}
}

void ad(ll v1) {
	st1.insert(-v1); sv1 += -v1;
	ev();
}

void dl(ll v1) {
	if (!st2.empty()&&*st2.begin() <= v1) {
		sv2 -= v1;
		st2.erase(st2.find(v1));
	}
	else {
		sv1 += v1;
		st1.erase(st1.find(-v1));
	}
	ev();
}

ll ans = -1;

int main() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		ad(a[i]);
		if (i > k) { dl(a[i - k]); }
		if (i >= k) {
			ll v1 = -*st1.begin()*st1.size() + sv1;
			v1 += sv2 - (-*st1.begin()*st2.size());
			ans = mn(ans, v1);
		}
	}
	cout << ans << endl;
	return 0;
}
0