题目大意: 这是一个关于牛的问题,需要确定需要多少个牛舍才能让每头母牛都有自己的挤奶时间段。母牛们只在某个精确的时间区间 A.,B(1≤A≤B≤1,000,000)内挤奶,其中包括时间 A 和 B。农夫约翰必须创建一个预约系统,以确定每头母牛可以在哪个牛舍里进行挤奶。同一时间段只能有一头牛挤奶。
帮助约翰确定:
- 谷仓需要的最少牛舍数量。
- 将这些牛分配到这些牛舍的时间。
主要就是对牛挤奶的开始时间进行排序,以便于衔接下一个区间。
如果对结束时间排序:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
const int N = 5e4 + 10;
const ll mod = 3e8 + 7;
int vis[N], sum = 0, xh[N], cnt = 1;
struct cow
{
int a, b, number;
bool operator<(struct cow const &c) const
{
if (a == c.a)
{
return b < c.b;
}
return a < c.a;
}
} cows[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> cows[i].a >> cows[i].b;
cows[i].number = i;
}
sort(cows, cows + n);
int qs = 0, mins = 0, f = 0;
while (sum != n)
{
for (int i = mins; i < n; i++)
{
if (vis[i] || cows[i].a <= qs)
{
if (!f)
{
mins = i, f = 1;
}
continue;
}
xh[cows[i].number] = cnt;
vis[i] = 1, sum++;//访问过
qs = cows[i].b;
}
cnt++;
f = qs = 0;
}
cout << cnt - 1 << endl;
for (int i = 0; i < n; i++)
{
cout << xh[i] << endl;
}
return 0;
}