Intervals

                                                                  Intervals


Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that there does not exist three intervals a, b and c such that a intersects with b, b intersects with c and c intersects with a.

Chiaki is interested in the minimum number of intervals which need to be deleted.

Note that interval a intersects with interval b if there exists a real number x such that laxra and lbxrb.


Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1 ≤ n ≤ 50000) -- the number of intervals.

Each of the following n lines contains two integers li and ri (1 ≤ li < ri ≤ 109) denoting the i-th interval. Note that for every 1 ≤ i < jn, lilj or rirj.

It is guaranteed that the sum of all n does not exceed 500000.

Output

For each test case, output an integer m denoting the minimum number of deletions. Then in the next line, output m integers in increasing order denoting the index of the intervals to be deleted. If m equals to 0, you should output an empty line in the second line.

Sample Input
1
11
2 5
4 7
3 9
6 11
1 12
10 15
8 17
13 18
16 20
14 21
19 22
Sample Output
4
3 5 7 10

/*谁的手最长,谁就对后面区间的影响最大。所以,若剔除,就剔除手最长的区间。

因为最后任意三个区间都不要求相交,所以从前向后看,每三个区间都不两两相交(这个解释不太有说服力,请无视)*/

AC代码:

#include <iostream>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<deque>
#include<stack>
using namespace std;
struct interval{
    int head;
    int tail;
    int local;
}interval[50009];
bool cmp1(struct interval a,struct interval b)
{
    if(a.head==b.head)
        return a.tail<b.tail;
    return a.head<b.head;
}
bool cmp2(struct interval a,struct interval b)
{
    if(a.tail==b.tail)
        return a.head<b.head;
    return a.tail<b.tail;
}
bool cmp3(int a,int b)
{
    return a<b;
}
int main()
{
    int n,i,p,j,t,ans;
    int result[50009];
    struct interval check[3],mid;
    while(scanf("%d",&t)!=EOF)
    {
        for(i=1;i<=t;i++)
        {
            ans=0;
            scanf("%d",&n);
            for(j=0;j<n;j++)
            {
                scanf("%d%d",&interval[j].head,&interval[j].tail);
                interval[j].local=j+1;
            }
            sort(interval,interval+n,cmp1);
            check[0]=interval[0];
            check[1]=interval[1];
            for(j=2;j<=n;j++)
            {
                check[2]=interval[j];
                sort(check,check+3,cmp1);
                if(check[0].tail>=check[1].head&&check[2].head<=check[1].tail&&check[2].head<=check[0].tail)
                {
                    sort(check,check+3,cmp2);
                    result[ans]=check[2].local;
                    ans++;
                }
                else
                {
                    sort(check,check+3,cmp2);
                    mid=check[0];
                    check[0]=check[2];
                    check[2]=mid;
                }
            }
            sort(result,result+ans,cmp3);
            printf("%d\n",ans);
            for(j=0;j<ans;j++)
            {
                if(j==0)
                    printf("%d",result[j]);
                else
                    printf(" %d",result[j]);
            }
            putchar('\n');
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值