結果

問題 No.738 平らな農地
ユーザー FF256grhyFF256grhy
提出日時 2018-09-29 04:40:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 3,339 bytes
コンパイル時間 1,662 ms
コンパイル使用メモリ 166,088 KB
実行使用メモリ 93,612 KB
最終ジャッジ日時 2023-08-02 12:32:30
合計ジャッジ時間 14,188 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 6 ms
4,816 KB
testcase_06 AC 7 ms
5,180 KB
testcase_07 AC 7 ms
5,156 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 5 ms
4,756 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 146 ms
39,804 KB
testcase_16 AC 151 ms
41,856 KB
testcase_17 AC 178 ms
40,012 KB
testcase_18 AC 155 ms
39,664 KB
testcase_19 AC 186 ms
41,656 KB
testcase_20 AC 167 ms
42,300 KB
testcase_21 AC 193 ms
42,048 KB
testcase_22 AC 190 ms
41,948 KB
testcase_23 AC 212 ms
43,868 KB
testcase_24 AC 216 ms
41,776 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 3 ms
4,384 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 4 ms
4,376 KB
testcase_33 AC 4 ms
4,376 KB
testcase_34 AC 4 ms
4,380 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 4 ms
4,376 KB
testcase_37 AC 4 ms
4,384 KB
testcase_38 AC 3 ms
4,380 KB
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 3 ms
4,380 KB
testcase_44 AC 3 ms
4,380 KB
testcase_45 AC 167 ms
48,152 KB
testcase_46 AC 148 ms
43,940 KB
testcase_47 AC 175 ms
46,340 KB
testcase_48 AC 152 ms
44,148 KB
testcase_49 AC 146 ms
45,892 KB
testcase_50 AC 157 ms
46,712 KB
testcase_51 AC 172 ms
48,132 KB
testcase_52 AC 161 ms
43,992 KB
testcase_53 AC 154 ms
44,424 KB
testcase_54 AC 181 ms
45,620 KB
testcase_55 AC 195 ms
47,020 KB
testcase_56 AC 173 ms
46,700 KB
testcase_57 AC 154 ms
43,792 KB
testcase_58 AC 169 ms
44,744 KB
testcase_59 AC 175 ms
47,992 KB
testcase_60 AC 153 ms
46,832 KB
testcase_61 AC 160 ms
47,116 KB
testcase_62 AC 153 ms
43,136 KB
testcase_63 AC 209 ms
47,488 KB
testcase_64 AC 191 ms
47,180 KB
testcase_65 AC 196 ms
41,796 KB
testcase_66 AC 203 ms
41,552 KB
testcase_67 AC 154 ms
88,928 KB
testcase_68 AC 158 ms
89,040 KB
testcase_69 AC 222 ms
89,440 KB
testcase_70 AC 188 ms
92,304 KB
testcase_71 AC 69 ms
4,380 KB
testcase_72 AC 84 ms
4,380 KB
testcase_73 AC 74 ms
4,376 KB
testcase_74 AC 89 ms
4,376 KB
testcase_75 AC 201 ms
86,452 KB
testcase_76 AC 192 ms
90,908 KB
testcase_77 AC 207 ms
86,052 KB
testcase_78 AC 235 ms
92,784 KB
testcase_79 AC 224 ms
93,612 KB
testcase_80 AC 195 ms
93,120 KB
testcase_81 AC 211 ms
88,244 KB
testcase_82 AC 215 ms
91,836 KB
testcase_83 AC 80 ms
4,380 KB
testcase_84 AC 78 ms
4,380 KB
testcase_85 AC 234 ms
66,744 KB
testcase_86 AC 226 ms
60,976 KB
testcase_87 AC 143 ms
64,148 KB
testcase_88 AC 140 ms
61,308 KB
testcase_89 AC 2 ms
4,376 KB
testcase_90 AC 1 ms
4,376 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> 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