結果

問題 No.738 平らな農地
ユーザー FF256grhy
提出日時 2018-09-29 04:40:49
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 305 ms / 2,000 ms
コード長 3,339 bytes
コンパイル時間 1,594 ms
コンパイル使用メモリ 168,704 KB
実行使用メモリ 93,440 KB
最終ジャッジ日時 2024-10-12 08:00:48
合計ジャッジ時間 15,085 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 87
権限があれば一括ダウンロードができます

ソースコード

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> struct Node { T s; int l, r; };

// とにかくひどい
const int D = 30;
const int Q = 200000;
Node<LL> v[2][D * Q];
int cnt;
template<typename T> class Tree {
private:
	int z;
	// vector<Node<T>> v;
public:
	Tree(int zz) { z = zz; cnt = 1; }
	T insert(T x) { return add(x,  1); }
	T  erase(T x) { return add(x, -1); }
	T   rank(T x) { return add(x,  0); }
	T add(T x, T y) { // v[x] += y して sum [0, x) を返す
		int p = 0;
		T sum = 0;
		dec(i, D) {
			bool is_l = ! ((x >> i) & 1);
			if(is_l) { v[z][p].s += y; } else { sum += v[z][p].s; }
			int & ref (is_l ? v[z][p].l : v[z][p].r);
			if(ref == 0) {
				if(y == 0) { return sum; }
				// ref = v.size();
				// v.PB({ 0, 0, 0 });
				ref = cnt;
				cnt++;
			}
			p = ref;
		}
		return sum;
	}
	T find(T s) { // sum[0, x) <= s となる最大の x を返す(最大値を投げると死ぬ気がする)
		int p = 0;
		T sum = 0, x = 0;
		dec(i, D) {
			if(sum + v[z][p].s > s) {
				p = v[z][p].l;
			} else {
				sum += v[z][p].s;
				p = v[z][p].r;
				x += 1LL << i;
			}
			assert(p != 0);
		}
		return x;
	}
};

// ----

// なんか n < 1600 くらいまでしかうまく動かない 

LL n, k, a[100000];

int main() {
	cin >> n >> k;
	inc(i, n) { cin >> a[i]; }
	
	Tree<LL> tc(0);
	Tree<LL> ts(1);
	LL INF = 1e9 + 1;
	tc.insert(INF);
	ts.insert(INF);
	
	LL ans = 1e15;
	inc(i, n) {
		LL x = a[i];
		tc.add(x, 1);
		ts.add(x, x);
		if(i >= k - 1) {
			LL M = tc.find(k / 2);
			LL H = INF;
			LL lc = tc.rank(M);
			LL ls = ts.rank(M);
			LL hc = tc.rank(H) - lc;
			LL hs = ts.rank(H) - ls;
			setmin(ans, (M * lc - ls) + (hs - M * hc));
			
			// cout << (M * lc - ls) << " + " << (hs - M * hc) << endl;
			
			LL y = a[i - k + 1];
			tc.add(y, -1);
			ts.add(y, -y);
		}
	}
	
	cout << ans << endl;
	
	return 0;
}
0