結果

問題 No.738 平らな農地
ユーザー bal4u
提出日時 2019-08-04 17:08:58
言語 C
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 2,519 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 32,640 KB
実行使用メモリ 9,324 KB
最終ジャッジ日時 2024-07-08 10:47:47
合計ジャッジ時間 71,179 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 53 WA * 4 TLE * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:10:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   10 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:16:24: note: in expansion of macro 'gc'
   16 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: No.738 平らな農地
// 2019.8.4 bal4u

#include <stdio.h>
#include <stdlib.h>

typedef long long ll;

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

// 数値のハッシュ関数
#define HASHSIZ 2000081
typedef struct { int fr, to; } HASH;
HASH hash[HASHSIZ+5], *hashend = hash+HASHSIZ;

int hlookup(int to) {
	HASH *p = hash + to % HASHSIZ;
	while (p->to) {
		if (p->to == to) break;
		if (++p == hashend) p = hash;
	}
	return p->fr;
}

void hinsert(int fr, int to) {
	HASH *p = hash + to % HASHSIZ;
	while (p->to) {
		if (p->to == to) return;
		if (++p == hashend) p = hash;
	}
	p->fr = fr, p->to = to;
}

// データ圧縮
#define MAX 100005
typedef struct { int a, id; } T;
T t[MAX];
int a[MAX], b[MAX]; int N;

int cmp(const void *a, const void *b) { return ((T *)a)->a - ((T *)b)->a; }

int compact(int n, int *to, int *fr) {
	int i, v;
	for (i = 0; i < n; i++) t[i].a = fr[i], t[i].id = i;
	qsort(t, n, sizeof(T), cmp);
	v = 1; to[t[0].id] = v;
	hinsert(t[0].a, v);
	for (i = 1; i < n; i++) {
		if (t[i].a != t[i-1].a) {
			v++;
			to[t[i].id] = v;
			hinsert(t[i].a, v);
		} else to[t[i].id] = v;
	}
	return v;
}

// bit木
int bit[MAX], bitn, bitp;

void init(int maxVal) {
	bitn = maxVal;
	bitp = 1; while (bitp < bitn) bitp <<= 1;
}

void add(int i, int x) {
	i++; while (i <= bitn) bit[i] += x, i += i & -i;
}

void insert(int val) { add(val, 1); }
void erase(int val) { add(val, -1); }
int nth_element(int n) {
	int a = 0, q = bitp;
	while (q >>= 1)
    if (a+q <= bitn && bit[a+q] <= n) a += q, n -= bit[a];
	return a;
}

#define ABS(x)  ((x)>=0?(x):-(x))

ll median(int m, int l, int r) {
	ll s = 0; while (l <= r) s += ABS(a[l]-m), l++;
	return s;
}

int main()
{
	int i, k, K, m;
	ll ans, t;

	N = in(), K = in();
	if (K == 1) { puts("0"); return 0; }
	if (K == 2) {
		m = in(), ans = 0x7fffffff;
		while (--N) {
			k = m, m = in();
			if ((t = ABS(k-m)) < ans) ans = t;
		}
		goto done;
	}
	
	k = K >> 1;
	for (i = 0; i < N; i++) a[i] = in();
	init(compact(N, b, a));
	
	for (i = 0; i < K; i++) insert(b[i]);
	m = hlookup(nth_element(k));
	ans = median(m, 0, K-1);
	if (ans == 0) goto done;
	
	t = ans;
	for (i = K; i < N; i++) {
		erase(b[i-K]), insert(b[i]);
		m = hlookup(nth_element(k));
		t = median(m, i-K+1, i);
		if (t < ans) {
			ans = t;
			if (ans == 0) goto done;
		}
	}
done:
	printf("%lld\n", ans);
	return 0;
}
0