結果

問題 No.2028 Even Choice
ユーザー 👑 ygussanyygussany
提出日時 2022-07-24 10:21:01
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,338 bytes
コンパイル時間 452 ms
コンパイル使用メモリ 29,592 KB
実行使用メモリ 5,228 KB
最終ジャッジ日時 2023-09-20 08:29:02
合計ジャッジ時間 3,340 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
4,380 KB
testcase_01 AC 38 ms
4,380 KB
testcase_02 AC 40 ms
4,380 KB
testcase_03 AC 30 ms
5,228 KB
testcase_04 AC 30 ms
4,684 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 35 ms
4,380 KB
testcase_07 AC 10 ms
4,376 KB
testcase_08 AC 22 ms
4,380 KB
testcase_09 AC 27 ms
4,380 KB
testcase_10 AC 9 ms
4,376 KB
testcase_11 AC 7 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 26 ms
4,688 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 0 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 0 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 0 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 18 ms
4,380 KB
testcase_29 AC 12 ms
4,380 KB
testcase_30 AC 9 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct {
	int key, id;
} data;

typedef struct {
	data obj[1000001];
	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 chmax(long long* a, long long b)
{
	if (*a < b) *a = b;
}

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