题解
题目大意 给你若干个区间 让你分为两个集合 要求两个集合的交集为空
按照l排序 记录之前出现过的最大的r 如果当前l没有覆盖着之前的r则从当前位置为分割点 前面为一组后面为一组
AC代码
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 10;
struct node
{
int l, r, i;
bool operator < (const node &oth) const
{
return l < oth.l;
}
}a[MAXN];
bool cmp(const node &a, const node &b)
{
return a.i < b.i;
}
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
scanf("%d%d", &a[i].l, &a[i].r), a[i].i = i;
sort(a + 1, a + n + 1); //按照l排序
int k = 0, r = a[1].r; //最远的r
for (int i = 2; i <= n; i++)
{
if (a[i].l > r) //当前位置没覆盖到之前的r
k = a[i].l; //记录可行的l
r = max(r, a[i].r);
}
sort(a + 1, a + n + 1, cmp);
if (k)
for (int i = 1; i <= n; i++) //大于等于k所记录的分为第二组
cout << (a[i].l >= k ? 2 : 1) << " ";
else
cout << -1;
cout << endl;
}
return 0;
}