結果

問題 No.738 平らな農地
ユーザー bal4ubal4u
提出日時 2019-08-04 17:08:58
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 2,519 bytes
コンパイル時間 1,239 ms
コンパイル使用メモリ 31,868 KB
実行使用メモリ 8,308 KB
最終ジャッジ日時 2023-09-22 20:05:11
合計ジャッジ時間 7,727 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 0 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:10:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:16:24: 備考: 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