CodeForces - 1101C - Division and Union

出自我自己的寒假康复训练,没康复成倒是wa了n多发,谨以此记。

C. Division and Union

There are nn segments [li,ri][li,ri] for 1≤i≤n1≤i≤n. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group.

To optimize testing process you will be given multitest.

Input

The first line contains one integer TT (1≤T≤500001≤T≤50000) — the number of queries. Each query contains description of the set of segments. Queries are independent.

First line of each query contains single integer nn (2≤n≤1052≤n≤105) — number of segments. It is guaranteed that ∑n∑n over all queries does not exceed 105105.

The next nn lines contains two integers lili, riri per line (1≤li≤ri≤2⋅1051≤li≤ri≤2⋅105) — the ii-th segment.

Output

For each query print nn integers t1,t2,…,tnt1,t2,…,tn (ti∈{1,2}ti∈{1,2}) — for each segment (in the same order as in the input) titi equals 11 if the ii-th segment will belongs to the first group and 22 otherwise.

If there are multiple answers, you can print any of them. If there is no answer, print −1−1.

Example

input

3
2
5 5
2 3
3
3 5
2 3
2 3
3
3 3
4 4
5 5

output

2 1 
-1
1 1 2 

Note

In the first query the first and the second segments should be in different groups, but exact numbers don't matter.

In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is −1−1.

In the third query we can distribute segments in any way that makes groups non-empty, so any answer of 66 possible is correct.

 

题意:这个题目就是给你n个范围[Li,Ri],然后把这些范围分成两堆(不能为空),每堆和另外一堆都不能有重合,最后输出每一个范围放在哪一堆(没有输出-1)。

分析:我们先将这些范围按照L从小到大排序,然后依次放到堆1,堆2,先放1再放2,再放1,不断重复。其中如果放1或者2不可以的话就放回原来那堆。例如前一个范围放的是堆1,现在的范围本应该放堆2,但是放堆2会和堆1冲突,于是我们将其放回堆1(放了肯定不会产生冲突)。依次类推直到放完所有的,如果最后有一个堆为空就说明不存在解,输出-1。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

struct node
{
	int l,r;
	int id;
}a[100005];

//lmax控制堆1的最大值,rmax控制堆2的最大值
int ans[100005],lmax,rmax;

//按照L升序,L相同R小的在前
int cmp(node a1,node a2)
{
	if (a1.l==a2.l)
		return a1.r<a2.r;
	else
		return a1.l<a2.l;
}

//flag1标记堆1不为空,flag2标记堆2不为空
int T,n,flag1,flag2;

int main()
{
	cin >> T;
	while(T--)
	{
		lmax=rmax=0; flag1=flag2=0;
		scanf("%d",&n); a[0].id=2;
		//记录id
		for (int i=1;i<=n;i++)
		{
			scanf("%d%d",&a[i].l,&a[i].r);
			a[i].id=i;
		}
		sort(a+1,a+n+1,cmp);
		for (int i=1;i<=n;i++)
		{
			//放堆1
			if (i==1 || ans[a[i-1].id]==2)
			{
				//放堆1不冲突
				if (a[i].l>rmax)
				{
					ans[a[i].id]=1;
					//重要!!!一开始只写了a[i].r,而没有考虑到a[i].r可能小于a[i-1].r
					lmax=max(lmax,a[i].r);
					flag1=1;
				}
				//冲突,放堆2
				else
				{
					ans[a[i].id]=2;
					rmax=max(rmax,a[i].r);
					flag2=1;
				}
			}
			//放堆2
			else
			{
				//放堆2不冲突
				if (a[i].l>lmax)
				{
					ans[a[i].id]=2;
					rmax=max(rmax,a[i].r);
					flag2=1;
				}
				//冲突,放堆1
				else
				{
					ans[a[i].id]=1;
					lmax=max(lmax,a[i].r);
					flag1=1;
				}
			}
		}
		//两堆都有
		if (flag1 && flag2)
		{
			for (int i=1;i<=n;i++)
				printf("%d ",ans[i]);
			printf("\n");
		}
		//只有1堆
		else
			printf("-1\n");
	}
	return 0;
}

 

转载于:https://www.cnblogs.com/Radium1209/p/10415330.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值