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 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.
OutputFor 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 Input1 11 2 5 4 7 3 9 6 11 1 12 10 15 8 17 13 18 16 20 14 21 19 22Sample 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;
}