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.
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
题意:要使任意三个区间不相交,求最少要删去几个区间,输出删去区间的输入序号。
思路:按照区间左边界从小到大排序,每次在三个区间之间比较,若符合相交条件,则将三个区间按右区间从大到小排序,将第一个区间设为要删去的区间(右边界最大)。
需要注意的是,若不符合相交条件,需要将三个区间按右区间从小到大排序,目的是充分检验下一个目标是否有三区间相交的情况。
我这里用了优先队列输出删掉区间的序号。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 500005
using namespace std;
priority_queue<int,vector<int>,greater<int> > Q;
typedef struct{
int L,R,num;
}node;
node P[maxn];
bool cmp(node x,node y){
if(x.L<y.L)
return true;
if(x.L==y.L&&x.R<y.R){
return true;
}
return false;
}
bool cmpx(node x,node y){
if(x.R<y.R)
return true;
return false;
}
bool cmpy(node x,node y){
if(x.R>y.R)
return true;
return false;
}
bool intersect(node a,node b,node c){
if(b.L<=a.R&&c.L<=b.R&&c.L<=a.R)
return true;
return false;
}
int main(){
int T,N;
int i,j,k;
int bid;
int sec,tri,counts;
scanf("%d",&T);
while(T--){
counts=0;
scanf("%d",&N);
for(i=1;i<=N;i++){
scanf("%d %d",&P[i].L,&P[i].R);
P[i].num=i;
}
sort(P+1,P+1+N,cmp);
for(i=3;i<=N;i++){
sort(P+i-2,P+i+1,cmp);
if(intersect(P[i-2],P[i-1],P[i])){
sort(P+i-2,P+i+1,cmpy);
Q.push(P[i-2].num);
counts++;
}
else{
sort(P+i-2,P+i+1,cmpx);
}
}
printf("%d\n",counts);
for(i=1;i<=counts;i++){
if(i<counts){
printf("%d ",Q.top());
Q.pop();
}
else{
printf("%d\n",Q.top());
Q.pop();
}
}
if(counts==0)
printf("\n");
}
return 0;
}