知识点:贪心
贪心的策略,用优先队列维护栅栏,每次都让结束时间最小的栅栏在堆顶,如果堆顶的不满足要求那么新建立一个栅栏入堆,
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5;
struct node {
int x, y, r;
} a[N];
bool cmp(node a, node b) {
return a.x < b.x;
}
struct cmp2 {
bool operator () (pair<int, int> a, pair<int, int> b) {
if (a.second != b.second) return a.second > b.second;
else return a.first > b.first;
}
};
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i].x >> a[i].y;
a[i].r = i;
}
sort(a, a + n, cmp);
int ans[N];
int tot = 1;
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp2> q;
q.push(make_pair(tot, a[0].y));
ans[a[0].r] = tot++;
for (int i = 1; i < n; i++) {
pair<int, int> t = q.top();
if (t.second < a[i].x) {
q.pop();
t.second = a[i].y;
q.push(t);
ans[a[i].r] = t.first;
} else {
q.push(make_pair(tot, a[i].y));
ans[a[i].r] = tot++;
}
}
cout << tot - 1 << endl;
for (int i = 0; i < n; i++) cout << ans[i] << endl;
return 0;
}