結果

問題 No.2304 Distinct Elements
ユーザー 👑 ygussanyygussany
提出日時 2023-04-29 18:32:56
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 115 ms / 3,000 ms
コード長 4,041 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 33,152 KB
実行使用メモリ 38,528 KB
最終ジャッジ日時 2024-04-29 10:01:36
合計ジャッジ時間 4,610 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 51 ms
5,376 KB
testcase_05 AC 52 ms
5,376 KB
testcase_06 AC 50 ms
5,376 KB
testcase_07 AC 51 ms
5,376 KB
testcase_08 AC 52 ms
5,376 KB
testcase_09 AC 113 ms
38,528 KB
testcase_10 AC 115 ms
38,400 KB
testcase_11 AC 114 ms
38,528 KB
testcase_12 AC 114 ms
38,528 KB
testcase_13 AC 114 ms
38,528 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 37 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 30 ms
5,376 KB
testcase_32 AC 90 ms
30,720 KB
testcase_33 AC 7 ms
5,376 KB
testcase_34 AC 92 ms
31,360 KB
testcase_35 AC 103 ms
34,944 KB
testcase_36 AC 6 ms
5,376 KB
testcase_37 AC 17 ms
5,376 KB
testcase_38 AC 54 ms
5,376 KB
testcase_39 AC 55 ms
5,376 KB
testcase_40 AC 31 ms
5,376 KB
testcase_41 AC 17 ms
5,376 KB
testcase_42 AC 40 ms
5,376 KB
testcase_43 AC 50 ms
5,376 KB
testcase_44 AC 81 ms
28,160 KB
testcase_45 AC 61 ms
20,096 KB
testcase_46 AC 47 ms
16,384 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 1 ms
5,376 KB
testcase_49 AC 49 ms
5,376 KB
testcase_50 AC 52 ms
5,376 KB
testcase_51 AC 51 ms
5,376 KB
testcase_52 AC 43 ms
5,376 KB
testcase_53 AC 34 ms
5,376 KB
testcase_54 AC 41 ms
5,376 KB
testcase_55 AC 40 ms
5,376 KB
testcase_56 AC 40 ms
5,376 KB
testcase_57 AC 73 ms
12,288 KB
testcase_58 AC 70 ms
12,160 KB
testcase_59 AC 41 ms
5,376 KB
testcase_60 AC 1 ms
5,376 KB
testcase_61 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void merge_sort(int n, int x[])
{
	static int y[200001] = {};
	if (n <= 1) return;
	merge_sort(n / 2, &(x[0]));
	merge_sort((n + 1) / 2, &(x[n/2]));
	
	int i, p, q;
	for (i = 0, p = 0, q = n / 2; i < n; i++) {
		if (p >= n / 2) y[i] = x[q++];
		else if (q >= n) y[i] = x[p++];
		else y[i] = (x[p] < x[q])? x[p++]: x[q++];
	}
	for (i = 0; i < n; i++) x[i] = y[i];
}

typedef struct {
	int key, id, left;
} data;

typedef struct {
	data obj[400001];
	int size;
} max_heap;

void push(max_heap* h, data x)
{
	int i = ++(h->size), j = i >> 1;
	data tmp;
	h->obj[i] = x;
	while (j > 0) {
		if (h->obj[i].key > h->obj[j].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j >>= 1;
		} else break;
	}
}

data pop(max_heap* h)
{
	int i = 1, j = 2;
	data output = h->obj[1], tmp;
	h->obj[1] = h->obj[(h->size)--];
	while (j <= h->size) {
		if (j < h->size && h->obj[j^1].key > h->obj[j].key) j ^= 1;
		if (h->obj[j].key > h->obj[i].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j <<= 1;
		} else break;
	}
	return output;
}

typedef struct {
	data *obj;
	int size, max_size, thr;
} min_heap;

void init_min_heap(min_heap* h, int s)
{
	h->max_size = s;
	h->size = 0;
	h->thr = 0;
	h->obj = (data*)malloc(sizeof(data) * (h->max_size + 1));
}

void min_push(min_heap* h, data x)
{
	int i = ++(h->size), j = i >> 1;
	data tmp;
	if (h->size > h->max_size) {
		h->max_size <<= 1;
		h->obj = (data*)realloc(h->obj, sizeof(data) * (h->max_size + 1));
	}
	h->obj[i] = x;
	while (j > 0) {
		if (h->obj[i].key < h->obj[j].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j >>= 1;
		} else break;
	}
}

data min_pop(min_heap* h)
{
	int i = 1, j = 2;
	data output = h->obj[1], tmp;
	h->obj[1] = h->obj[(h->size)--];
	while (j <= h->size) {
		if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1;
		if (h->obj[j].key < h->obj[i].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j <<= 1;
		} else break;
	}
	return output;
}

void merge_min_heap(min_heap* h, min_heap* hh)
{
	int i;
	data d;
	if (h->size >= hh->size) {
		for (i = 1; i <= hh->size; i++) {
			d = hh->obj[i];
			d.key += h->thr - hh->thr;
			min_push(h, d);
		}
		free(hh->obj);
	} else {
		for (i = 1; i <= h->size; i++) {
			d = h->obj[i];
			d.key += hh->thr - h->thr;
			min_push(hh, d);
		}
		free(h->obj);
		*h = *hh;
	}
}

int main()
{
	int i, N, A[200001];
	scanf("%d", &N);
	for (i = 0; i < N; i++) scanf("%d", &(A[i]));
	merge_sort(N, A);
	
	int j, k;
	long long ans = 0;
	max_heap h;
	min_heap he[200001];
	data d, e;
	h.size = 0;
	d.key = A[0];
	d.left = A[0];
	d.id = 0;
	push(&h, d);
	init_min_heap(&(he[0]), 10);
	for (i = 1; i < N; i++) {
		d = pop(&h);
		if (A[i] > d.key + 1) {
			push(&h, d);
			d.key = A[i];
			d.left = A[i];
			d.id = i;
			push(&h, d);
			init_min_heap(&(he[i]), 10);
		} else if (A[i] == d.key + 1) {
			d.key++;
			push(&h, d);
		} else {
			j = d.id;
			while (he[j].size > 0 && he[j].obj[1].key <= he[j].thr) min_pop(&(he[j]));
			if (abs(d.key - A[i]) - he[j].size + (d.key - d.left + 1 - he[j].size) < abs(d.key + 1 - A[i])) {
				ans += abs(d.key - A[i]) - he[j].size + (d.key - d.left + 1 - he[j].size);
				d.left--;
				he[j].thr++;
				if (h.size > 0 && h.obj[1].key == d.left - 1) {
					h.obj[1].key = d.key;
					merge_min_heap(&(he[h.obj[1].id]), &(he[j]));
					e.key = d.key - A[i] + he[h.obj[1].id].thr;
					min_push(&(he[h.obj[1].id]), e);
				} else {
					push(&h, d);
					e.key = d.key - A[i] + he[j].thr;
					min_push(&(he[j]), e);
				}
			} else {
				ans += abs(d.key + 1 - A[i]);
				d.key++;
				push(&h, d);
				e.key = d.key - A[i] + he[j].thr;
				min_push(&(he[j]), e);
			}
		}
	}
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}
0