## POJ3190贪心

• 使每只牛都有专属时间的最小牛棚数
• 每只牛在哪个牛棚

```#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<queue>
using namespace std;

struct Cow {
int s, e, id;
}cow[50500];
struct cow_home {
int finish;
int home_id;
cow_home() {}
cow_home(int finish, int home_id) :finish(finish), home_id(home_id) {}
bool operator < (const cow_home& a)const
{
return finish > a.finish;
}
};
bool cmp(Cow a, Cow b) {
if (a.s == b.s)return a.e > b.e;
return a.s < b.s;
}
int result_home[50500];
int main(void)
{
ios::sync_with_stdio(false);
int N;
cin >> N;
for (int i = 1; i <= N; i++)
{
cin >> cow[i].s >> cow[i].e;
cow[i].id = i;
}
sort(cow + 1, cow + 1 + N, cmp);
int num_home = 1;
priority_queue<cow_home>q;
q.push(cow_home(cow[1].e, 1));
result_home[cow[1].id] = 1;
for (int i = 2; i <= N; i++)
{
cow_home now;
now = q.top();
if (cow[i].s > now.finish)//不能取等，题目要求不能重叠
{
q.pop();
result_home[cow[i].id] = now.home_id;
q.push(cow_home(cow[i].e, now.home_id));
}
else
{
num_home++;
result_home[cow[i].id] = num_home;
q.push(cow_home(cow[i].e, num_home));
}
}
cout << num_home << endl;
for (int i = 1; i <= N; i++)
cout << result_home[i] << endl;
return 0;
}```