結果

問題 No.2304 Distinct Elements
ユーザー 👑 ygussanyygussany
提出日時 2023-04-29 17:50:33
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 2,140 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 32,000 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2024-04-29 09:42:11
合計ジャッジ時間 6,422 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
} 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;
}

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, mate[200001];
	max_heap h;
	data d;
	long long ans = 0;
	h.size = 0;
	mate[0] = 0;
	d.key = A[0];
	d.id = A[0];
	push(&h, d);
	for (i = 1; i < N; i++) {
		d = pop(&h);
		if (A[i] >= d.key + 1) {
			push(&h, d);
			mate[i] = 0;
			d.key = A[i];
			d.id = A[i];
			if (h.size > 0 && h.obj[1].key == d.id - 1) {
				h.obj[1].key = d.key;
			} else push(&h, d);
		} else {
			for (j = i - (d.key - d.id + 1), k = 0; j < i; j++) if (mate[j] > 0) k++;
			if (abs(d.id - 1 - A[i]) - k < abs(d.key + 1 - A[i])) {
				for (j = i - (d.key - d.id + 1), k = 0; j < i; j++) mate[j]--;
				mate[i] = d.key - A[i];
				d.id--;
				if (h.size > 0 && h.obj[1].key == d.id - 1) {
					h.obj[1].key = d.key;
				} else push(&h, d);
			} else {
				mate[i] = ++d.key - A[i];
				push(&h, d);
			}
		}
	}
	for (i = 0; i < N; i++) ans += abs(mate[i]);
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}
0