結果

問題 No.738 平らな農地
ユーザー FF256grhyFF256grhy
提出日時 2018-09-29 05:29:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 442 ms / 2,000 ms
コード長 4,022 bytes
コンパイル時間 4,974 ms
コンパイル使用メモリ 180,592 KB
実行使用メモリ 15,240 KB
最終ジャッジ日時 2023-08-02 12:36:35
合計ジャッジ時間 21,004 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 6 ms
4,376 KB
testcase_06 AC 7 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 249 ms
12,440 KB
testcase_16 AC 257 ms
12,480 KB
testcase_17 AC 307 ms
12,672 KB
testcase_18 AC 306 ms
12,412 KB
testcase_19 AC 375 ms
12,996 KB
testcase_20 AC 264 ms
12,488 KB
testcase_21 AC 361 ms
12,792 KB
testcase_22 AC 269 ms
12,516 KB
testcase_23 AC 335 ms
12,612 KB
testcase_24 AC 339 ms
12,508 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 3 ms
4,384 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 2 ms
4,380 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,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 3 ms
4,376 KB
testcase_45 AC 245 ms
13,356 KB
testcase_46 AC 252 ms
12,804 KB
testcase_47 AC 260 ms
13,148 KB
testcase_48 AC 216 ms
12,988 KB
testcase_49 AC 220 ms
13,024 KB
testcase_50 AC 225 ms
13,092 KB
testcase_51 AC 272 ms
13,140 KB
testcase_52 AC 237 ms
12,864 KB
testcase_53 AC 258 ms
13,236 KB
testcase_54 AC 280 ms
13,208 KB
testcase_55 AC 293 ms
13,148 KB
testcase_56 AC 266 ms
13,048 KB
testcase_57 AC 235 ms
12,936 KB
testcase_58 AC 244 ms
13,148 KB
testcase_59 AC 242 ms
13,360 KB
testcase_60 AC 228 ms
13,508 KB
testcase_61 AC 246 ms
13,192 KB
testcase_62 AC 225 ms
12,872 KB
testcase_63 AC 261 ms
13,292 KB
testcase_64 AC 261 ms
13,316 KB
testcase_65 AC 390 ms
12,624 KB
testcase_66 AC 413 ms
12,916 KB
testcase_67 AC 222 ms
14,712 KB
testcase_68 AC 217 ms
14,720 KB
testcase_69 AC 323 ms
14,956 KB
testcase_70 AC 263 ms
15,240 KB
testcase_71 AC 260 ms
8,964 KB
testcase_72 AC 221 ms
8,816 KB
testcase_73 AC 197 ms
8,920 KB
testcase_74 AC 266 ms
8,992 KB
testcase_75 AC 300 ms
14,452 KB
testcase_76 AC 256 ms
14,768 KB
testcase_77 AC 326 ms
14,556 KB
testcase_78 AC 348 ms
15,076 KB
testcase_79 AC 347 ms
15,188 KB
testcase_80 AC 270 ms
14,844 KB
testcase_81 AC 299 ms
14,608 KB
testcase_82 AC 339 ms
14,980 KB
testcase_83 AC 241 ms
9,028 KB
testcase_84 AC 229 ms
8,916 KB
testcase_85 AC 442 ms
14,980 KB
testcase_86 AC 396 ms
14,348 KB
testcase_87 AC 129 ms
14,684 KB
testcase_88 AC 123 ms
14,372 KB
testcase_89 AC 1 ms
4,380 KB
testcase_90 AC 1 ms
4,380 KB
testcase_91 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

template<typename T> class SegmentTree {
private:
	T * a = NULL;
	int N = -1, S;
	function<T(T, T)> F;
	T I;
	bool is_available = false;
public:
	SegmentTree() { }
	SegmentTree(int n, function<T(T, T)> func, T id) { init(n, func, id); }
	void init(int size, function<T(T, T)> func, T id) {
		assert(size > 0);
		N = size;
		F = func;
		I = id;
		S = 1;
		while(S < size) { S *= 2; }
		delete[] a;
		a = new T[S * 2];
		inc(i, S * 2) { a[i] = I; }
		is_available = true;
	}
	T operator[](int p) {
		assert(inID(p, 0, N));
		p += S;
		return a[p];
	}
	T & ref(int p) {
		is_available = false;
		assert(inID(p, 0, N));
		p += S;
		return a[p];
	}
	void calc() {
		decID(i, 1, S) { a[i] = F(a[i * 2], a[i * 2 + 1]); }
		is_available = true;
	}
	void apply(int p, function<void(T &)> op) {
		assert(inID(p, 0, N));
		p += S;
		op(a[p]);
		while(p != 1) {
			p /= 2;
			a[p] = F(a[p * 2], a[p * 2 + 1]);
		}
	}
	T fold_ID(int l, int r, bool loop = false) {
		assert(is_available);
		assert(inII(l, 0, N));
		assert(inII(r, 0, N));
		if(loop && l >= r) { return F(fold_ID(l, N), fold_ID(0, r)); }
		assert(l <= r);
		l += S;
		r += S;
		T v = I, w = I;
		while(l < r) {
			if(l + 1 == r) { v = F(v, a[l]); break; }
			
			if(l % 2 == 1) { v = F(v, a[l]); }
			if(r % 2 == 1) { w = F(a[r - 1], w); }
			
			l = (l + 1) / 2;
			r = r / 2;
		}
		return F(v, w);
	}
	T fold_II(int l, int r, bool loop = false) { return fold_ID(l    , r + 1, loop); }
	T fold_CD(int l, int r, bool loop = false) { return fold_ID(l + 1, r    , loop); }
	T fold_CI(int l, int r, bool loop = false) { return fold_ID(l + 1, r + 1, loop); }
};

#define OP(op) [&](auto A, auto B) { return op; }
#define AP(op) [&](auto & A) { op; }

// ---- ----

LL n, k, a[100000], b[100000];
map<LL, LL> mp;

int main() {
	cin >> n >> k;
	inc(i, n) { cin >> a[i]; b[i] = a[i]; }
	
	sort(b, b + n);
	inc(i, n) { mp[b[i]] = i; }
	
	SegmentTree<pair<LL, LL>> st(n, OP(MP(A.FI + B.FI, A.SE + B.SE)), MP(0, 0));
	LL ans = 1e15;
	inc(i, n) {
		st.apply(mp[a[i]], AP(A.FI++; A.SE += a[i]));
		if(i >= k - 1) {
			int L = -1, H = n;
			while(H - L > 1) {
				int M = (H + L) / 2;
				(st.fold_II(0, M).FI <= k / 2 ? L : H) = M;
			}
			int m = H;
			
			auto ll = st.fold_ID(0, m);
			auto hh = st.fold_ID(m, n);
			LL lc = ll.FI;
			LL ls = ll.SE;
			LL hc = hh.FI;
			LL hs = hh.SE;
			setmin(ans, (b[m] * lc - ls) + (hs - b[m] * hc));
			
			st.apply(mp[a[i - k + 1]], AP(A.FI--; A.SE -= a[i - k + 1]));
		}
	}
	
	cout << ans << endl;
	
	return 0;
}
0