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 la ≤ x ≤ ra and lb ≤ x ≤ rb.
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 < j ≤ n, li ≠ lj or ri ≠ rj.
It is guaranteed that the sum of all n does not exceed 500000.
<h4< dd="">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.
<h4< dd="">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
<h4< dd="">Sample Output
4 3 5 7 10
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=50050;
struct node
{
	int l,r,id;
}ex[maxn];
int ans[maxn];
bool cmp1(node a,node b)
{
	if(a.l==b.l) return a.r<b.r;
	return a.l<b.l;
}
bool cmp2(node a,node b)
{
	if(a.r==b.r) return a.l<b.l;
	return a.r>b.r;
}
int isinterval(node x,node y,node z)
{
	int f1=0,f2=0;
	if(x.r>=y.l) f1=1;//x&y
	if(x.r>=z.l&&y.r>=z.l) f2=1;//z&x,z&y
    if(f1&&f2) return 1;
    return 0;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,pos=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++) 
		{
			scanf("%d%d",&ex[i].l,&ex[i].r);
			ex[i].id=i+1;
		}
		sort(ex,ex+n,cmp1);
		node x[5];
		x[0]=ex[0];
		x[1]=ex[1];
		for(int i=2;i<n;i++)
		{
			x[2]=ex[i];
		//	sort(x,x+3,cmp1); 
			int f=isinterval(x[0],x[1],x[2]);
			sort(x,x+3,cmp2);
			if(f)
			{
				ans[pos++]=x[0].id;
				swap(x[0],x[2]);
			}
		}
		sort(ans,ans+pos);
		printf("%d\n",pos);
		if(pos==0) printf("\n");
		else
		{
			for(int i=0;i<pos-1;i++) printf("%d ",ans[i]);
		    printf("%d\n",ans[pos-1]);
		}
	}
	return 0;
}
原文:https://www.cnblogs.com/EchoZQN/p/10372040.html