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;
}