結果

問題 No.1557 Binary Variable
ユーザー pengin_2000pengin_2000
提出日時 2022-01-18 17:02:09
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,158 bytes
コンパイル時間 384 ms
コンパイル使用メモリ 30,208 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-23 13:23:49
合計ジャッジ時間 6,889 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
int l[200005], r[200005];
int h[200005], hl;
int comp_h(int a, int b)
{
	if (r[h[a]] > r[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[hl] = ne;
	int p = hl;
	hl++;
	for (; p > 0; p = (p - 1) / 2)
		if (comp_h((p - 1) / 2, p) > 0)
			swap_h((p - 1) / 2, p);
	return;
}
int pop()
{
	hl--;
	swap_h(0, hl);
	int p = 0;
	for (;;)
	{
		if (2 * p + 2 < hl)
		{
			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 < hl)
		{
			if (comp_h(p, 2 * p + 1) > 0)
				swap_h(p, 2 * p + 1);
			p = 2 * p + 1;
		}
		else
			break;
	}
	return h[hl];
}
int main()
{
	int n, m;
	scanf("%d %d", &n, &m);
	int i;
	for (i = 0; i < m; i++)
		scanf("%d %d", &l[i], &r[i]);
	hl = 0;
	for (i = 0; i < m; i++)
		push(i);
	int max = 0;
	int ans = n;
	while (hl > 0)
	{
		i = pop();
		if (max < l[i])
		{
			ans--;
			max = r[i];
		}
	}
	printf("%d\n", ans);
	return 0;
}
0