結果

問題 No.2029 Swap Min Max Min
ユーザー 👑 ygussanyygussany
提出日時 2022-08-05 22:12:40
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,724 bytes
コンパイル時間 1,807 ms
コンパイル使用メモリ 30,492 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 00:11:01
合計ジャッジ時間 4,911 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 22 ms
4,352 KB
testcase_04 AC 6 ms
4,352 KB
testcase_05 AC 11 ms
4,352 KB
testcase_06 AC 23 ms
4,348 KB
testcase_07 AC 27 ms
4,348 KB
testcase_08 AC 36 ms
4,352 KB
testcase_09 AC 10 ms
4,348 KB
testcase_10 AC 7 ms
4,352 KB
testcase_11 AC 14 ms
4,348 KB
testcase_12 AC 14 ms
4,352 KB
testcase_13 AC 39 ms
4,352 KB
testcase_14 AC 33 ms
4,348 KB
testcase_15 AC 41 ms
4,352 KB
testcase_16 AC 32 ms
4,352 KB
testcase_17 AC 39 ms
4,352 KB
testcase_18 AC 44 ms
4,352 KB
testcase_19 AC 33 ms
4,352 KB
testcase_20 AC 33 ms
4,348 KB
testcase_21 AC 33 ms
4,348 KB
testcase_22 AC 32 ms
4,348 KB
testcase_23 AC 41 ms
4,348 KB
testcase_24 AC 41 ms
4,348 KB
testcase_25 AC 32 ms
4,348 KB
testcase_26 AC 32 ms
4,352 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 0 ms
4,352 KB
testcase_34 AC 0 ms
4,348 KB
testcase_35 AC 0 ms
4,352 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 1 ms
4,352 KB
testcase_38 AC 35 ms
4,348 KB
testcase_39 AC 31 ms
4,348 KB
testcase_40 AC 27 ms
4,348 KB
testcase_41 AC 37 ms
4,352 KB
testcase_42 AC 37 ms
4,348 KB
testcase_43 AC 30 ms
4,348 KB
testcase_44 AC 40 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef struct {
	int key, id;
} data;

typedef struct {
	data obj[200001];
	int size;
} min_heap;

void push(min_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(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 chmin(long long* a, long long b)
{
	if (*a > b) *a = b;
}

int main()
{
	int i, N, A[200001];
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d", &(A[i]));
	printf("%d ", N / 2);
	
	long long ans, tmp;
	data d;
	min_heap h;
	if (N % 2 == 0) {
		h.size = 0;
		for (i = 1; i <= N; i++) {
			if (A[i] <= N / 2) {
				d.key = i;
				push(&h, d);
			}
		}
		for (i = 1, ans = 0; i <= N / 2; i++) {
			d = pop(&h);
			ans += abs(i * 2 - 1 - d.key);
		}
		
		tmp = ans;
		h.size = 0;
		for (i = 1; i <= N; i++) {
			if (A[i] <= N / 2) {
				d.key = i;
				push(&h, d);
			}
		}
		for (i = 1; i <= N / 2; i++) {
			d = pop(&h);
			if (d.key >= i * 2) tmp--;
			else tmp++;
			chmin(&ans, tmp);
		}
	} else {
		h.size = 0;
		for (i = 1; i <= N; i++) {
			if (A[i] <= N / 2) {
				d.key = i;
				push(&h, d);
			}
		}
		for (i = 1, ans = 0; i <= N / 2; i++) {
			d = pop(&h);
			ans += abs(i * 2 - d.key);
		}
	}
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}
0