贪心
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int N = 50000 + 5;
struct S {
int l, r, id;
bool operator<(const S& s)const {
return l < s.l;
}
}s[N];
struct Node {
int val, id;
bool operator<(const Node& a)const {
return val > a.val;
}
};
int res[N];
int main() {
int n; scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d%d", &s[i].l, &s[i].r), s[i].id = i;
sort(s, s + n);
priority_queue<Node> q;
int cnt = 0;
for (int i = 0; i < n; i++) {
if (q.size()) {
Node tp = q.top();
if (s[i].l > tp.val) {
tp.val = s[i].r;
res[s[i].id] = tp.id;
q.pop();
q.push(tp);
}
else q.push({ s[i].r,++cnt }), res[s[i].id] = cnt;
}
else q.push({ s[i].r,++cnt }), res[s[i].id] = cnt;
}
printf("%d\n", q.size());
for (int i = 0; i < n; i++)
printf("%d\n", res[i]);
return 0;
}