C. 【例题3】畜栏预定

C . 【 例 题 3 】 畜 栏 预 定 C. 【例题3】畜栏预定 C.3


在这里插入图片描述
在这里插入图片描述


题解

考虑贪心


Code

#include <bits/stdc++.h>
using namespace std;

struct node
{
	int x, y;
}tr[100005];
int n, m, a[100005][3], ans[100005];

bool cmp (node x, node y)
{
	return x.x < y.x ? 1 : 0;
}

int main ()
{
	scanf ("%d", &n);
	for (int i = 1; i <= n; ++ i) scanf ("%d%d", &tr[i].x, &tr[i].y);
	sort (tr + 1, tr + 1 + n, cmp);
	for (int i = 1; i <= n; ++ i)
	{
		int t1 = tr[i].x, t2 = tr[i].y, f = 0;
		for (int j = 1; j <= m; ++j)
		{
			if (a[j][2] < t1)
			{
				f = 1;
				a[j][1] = t1; a[j][2] = t2; ans[i] = j;
				break;
			}
		}
		if (!f)
		{
			++ m;
			a[m][1] = t1; a[m][2] = t2; ans[i] = m;
		}
	}
	printf ("%d\n", m);
	for (int i = 1; i <= n; ++ i) printf ("%d\n", ans[i]);
	return 0;
}
posted @ 2021-01-03 08:44  unknown_future  阅读(73)  评论(0编辑  收藏  举报