cf Educational Codeforces Round 58 C. Division and Union

原题:
C. Division and Union

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

There are n segments [li,ri] for 1≤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 T (1≤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 n (2≤n≤105) — number of segments. It is guaranteed that ∑n over all queries does not exceed 10^5.

The next n lines contains two integers li, ri per line (1≤li≤ri≤2⋅105) — the i-th segment.

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

If there are multiple answers, you can print any of them. If there is no answer, print −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.

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

中文:

给你一堆区间,问你是否可以将这些区间分成两组,两组中任意选出的两个区间没有交集。如果不能,输出-1,否则输出每一个区间对应的组编号。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

struct node
{
    int l,r,i,ans;
};

const int maxn=200000;

node seg[maxn];
int t,l,r,n;

int cmp(const node &n1,const node &n2)
{
    return n1.l<n2.l;
}
int cmp_i(const node &n1,const node &n2)
{
    return n1.i<n2.i;
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>seg[i].l>>seg[i].r;
            seg[i].i=i;
        }
        sort(seg,seg+n,cmp);
        int flag=-1;


        int res=seg[0].r;
        for(int i=1;i<n;i++)
        {
            if(seg[i].l>res)
            {
                flag=i;
                break;
            }
            if(seg[i].l<=res)
            {
                if(seg[i].r>res)
                    res=seg[i].r;
            }
        }
        if(flag==-1)
            cout<<-1<<endl;
        if(flag>0)
        {
            for(int i=0;i<n;i++)
            {
                if(i<flag)
                    seg[i].ans=1;
                else
                    seg[i].ans=2;
            }
            sort(seg,seg+n,cmp_i);
            for(int i=0;i<n;i++)
            {
                if(i!=n-1)
                    cout<<seg[i].ans<<" ";
                else
                    cout<<seg[i].ans<<endl;
            }
        }
    }
    return 0;
}

思路:

这题的数据给定方式是先给出t表示有t个测试例,然后输入n个区间。如果不仔细读题会认为必须要在O(n)的时间复杂度求解才能满足要求,实际上题目中有这样一句话“It is guaranteed that ∑n over all queries does not exceed 10^5.” 使用时间复杂度O(nlogn)的算法就行。

对区间按照左端点排序,枚举每个区间是否与前一个区间相交,如果出现了当前区间与前一个区间不相交,那么就可以将当前区间以及其后面的区间划分为一组,剩下的划分为另一组即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值