結果

問題 No.2029 Swap Min Max Min
ユーザー 👑 ygussanyygussany
提出日時 2022-07-24 10:28:12
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,648 bytes
コンパイル時間 1,243 ms
コンパイル使用メモリ 30,332 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-14 00:02:42
合計ジャッジ時間 4,525 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 11 ms
4,352 KB
testcase_06 AC 22 ms
4,356 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 6 ms
4,348 KB
testcase_11 WA -
testcase_12 AC 14 ms
4,352 KB
testcase_13 WA -
testcase_14 AC 33 ms
4,348 KB
testcase_15 WA -
testcase_16 AC 32 ms
4,356 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 32 ms
4,352 KB
testcase_20 AC 34 ms
4,352 KB
testcase_21 AC 33 ms
4,348 KB
testcase_22 AC 33 ms
4,352 KB
testcase_23 AC 41 ms
4,352 KB
testcase_24 AC 41 ms
4,352 KB
testcase_25 AC 32 ms
4,348 KB
testcase_26 AC 31 ms
4,352 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 1 ms
4,356 KB
testcase_29 AC 0 ms
4,348 KB
testcase_30 AC 0 ms
4,348 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 WA -
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 1 ms
4,352 KB
testcase_38 AC 35 ms
4,348 KB
testcase_39 AC 31 ms
4,352 KB
testcase_40 AC 28 ms
4,348 KB
testcase_41 AC 38 ms
4,352 KB
testcase_42 AC 38 ms
4,348 KB
testcase_43 AC 30 ms
4,352 KB
testcase_44 AC 41 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;
}

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 - d.key);
		}
		
		h.size = 0;
		for (i = 1; i <= N; i++) {
			if (A[i] <= N / 2) {
				d.key = i;
				push(&h, d);
			}
		}
		for (i = 1, tmp = 0; i <= N / 2; i++) {
			d = pop(&h);
			tmp += abs(i * 2 - 1 - d.key);
		}
		if (ans > tmp) 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