思路:
贪心。
就先排序,然后设置个数组来表示栅栏里还有没有牛
如果当前的牛吃草的开始时间晚于先前任意一个畜栏放入最后牛的结束时间,
则我们将当前的牛安排进去,并更新最后结束时间;
如果没有,则为它新建一个畜栏。
C o d e Code Code:
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int ans[100000];
int a[100001][3];
struct node {int x, y;} p[100001];
bool cmp(node xh, node hy){return xh.x < hy.x;}//升序排序
int main ()
{
int m = 0;//方案
scanf ("%d", &n);
for (int i = 1; i <= n; ++i)
scanf ("%d%d", &p[i].x,&p[i].y);
sort (p + 1, p + 1 + n,cmp);
for (int i = 1; i <= n; ++i)//贪心
{
int x = p[i].x, y = p[i].y;
bool flag = 0;
for (int j = 1; j <= m; ++j)
if (a[j][2] < x)
{
flag = 1;
a[j][1] = x;
a[j][2] = y;
ans[i] = j;
break;
}
if(!flag)
{
m++;
a[m][1] = x;
a[m][2] = y;
ans[i] = m;
}
}
printf("%d\n",m);
for (int i = 1; i <= n; ++i)
printf("%d\n",ans[i]) ;
}