题解 CF1101C 【Division and Union】

题目link:
Codeforces
luogu
先按端点由小到大排序,然后把线段合并为一条(能并则并)
不能并呢?(两端点都在合并线段之外)
那么合并线段目前长度就是断点了!(有兴趣可以证明)
剩下的过程难度pj-
最后上代码


#include<bits/stdc++.h>
using namespace std;
struct node
{
	int l;
	int r;
}edge[100010];
int T,n,nowlen,POI,yl[100010],yr[100010];//yl,yr输出用,记录原来顺序
int cmp(node a,node b)
{
	return a.l<b.l;
}
int main()
{
	cin>>T;
	for(int js=1;js<=T;js++)
	{
		POI=-1;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>edge[i].l>>edge[i].r;
			yl[i]=edge[i].l;
			yr[i]=edge[i].r;
		}
		sort(edge+1,edge+n+1,cmp);
		nowlen=edge[1].l;
		for(int i=1;i<=n;i++)
		{
			if(edge[i].r>nowlen&&edge[i].l<=nowlen)
			{
				nowlen=edge[i].r;
			}
			else
			{
				if(edge[i].r>nowlen)
				{
					POI=nowlen;
					break;
				}
			}
		}
		if(POI==-1)
		{
			cout<<-1<<endl;
			continue;
		}
		for(int i=1;i<=n;i++)
		{
			if(yr[i]<=POI)
			{
				cout<<1<<" ";
			}
			else
			{
				cout<<2<<" ";
			}
		}
		cout<<endl;
	}
} 
posted @ 2019-02-11 21:01  G_A_TS  阅读(539)  评论(0)    收藏  举报