結果

問題 No.2334 Distinct Cards
ユーザー pengin_2000
提出日時 2023-06-02 21:29:26
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,162 bytes
コンパイル時間 656 ms
コンパイル使用メモリ 30,464 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-28 16:30:30
合計ジャッジ時間 1,365 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
int h[100005], l;
int comp_h(int a, int b)
{
	if (h[a] < h[b])
		return 1;
	else
		return -1;
}
void swap_h(int a, int b)
{
	int f = h[a];
	h[a] = h[b];
	h[b] = f;
	return;
}
void push(int ne)
{
	h[l] = ne;
	int p = l;
	l++;
	for (; p > 0; p = (p - 1) / 2)
		if (comp_h((p - 1) / 2, p) > 0)
			swap_h((p - 1) / 2, p);
	return;
}
int pop()
{
	l--;
	swap_h(0, l);
	int p = 0;
	for (;;)
	{
		if (2 * p + 2 < l)
		{
			if (comp_h(2 * p + 1, 2 * p + 2) > 0)
			{
				if (comp_h(p, 2 * p + 2) > 0)
					swap_h(p, 2 * p + 2);
				p = 2 * p + 2;
			}
			else
			{
				if (comp_h(p, 2 * p + 1) > 0)
					swap_h(p, 2 * p + 1);
				p = 2 * p + 1;
			}
		}
		else if (2 * p + 1 < l)
		{
			if (comp_h(p, 2 * p + 1) > 0)
				swap_h(p, 2 * p + 1);
			p = 2 * p + 1;
		}
		else
			break;
	}
	return h[l];
}
int a[100005];
int cnt[100005];
int main()
{
	int n, k;
	scanf("%d %d", &n, &k);
	int i;
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for (i = 0; i < n; i++)
		cnt[i] = 0;
	for (i = 0; i < n; i++)
		cnt[a[i] - 1]++;
	l = 0;
	for (i = 0; i < n; i++)
		push(cnt[i]);
	int ans = 0;
	for (; k > 0; ans++)
		k -= pop();
	printf("%d\n", ans);
	return 0;
}
0