Intervals
Time Limit: 1 Second Memory Limit: 65536 KB Special Judge
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
Author: LIN, Xi
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple
明显,问题就是求:删除至少多少个区间,使得不存在任意一点x被>=3个区间覆盖
先简化一下问题
假如问题改为,不存在x被>=2个区间覆盖
那直接贪心,求一次活动分配问题就行了,标记哪个区间被用掉了,没被用的全部是被删掉的
在已经按以上的方法 ,选择了一部分区间后
显然 ,再选择任意一个还没被选择的区间,必然会让某一点x,被2个区间覆盖
那再对剩余的区间求一次活动分配
这样 如果再选择剩余的任意区间,都会使得某一点x被3个区间覆盖
然而 如果遇到
[1,2] [1,3] [4,100] [3,101] 这样的情况
经典的活动分配问题会选出
[1,2] [4,100]
[1,3]
最后删掉[3,101]
而显然
[1,2] [3,101]
[1,3] [4,100]更优
那就修改一下算法
同时进行2个线程的活动分配
假如线程1的最晚活动结束时间为t1
线程2的最晚结束时间为t2
对一个待选择的活动,优先添加到t更大的线程后面(前提是该活动开始时间>t)
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<string.h>
#include<math.h>
#include<list>
using namespace std;
#define ll long long
#define pii pair<int,int>
const int inf = 1e9 + 7;
const int N = 50000 + 5;
struct S{
int l,r;
int idx;
}a[N];
bool used[N];
bool cmp(const S&a,const S&b){
if(a.r!=b.r){
return a.r<b.r;//结束时间升序
}
return a.l>b.l;//开始时间递减序
}
vector<int>ans;
void slove(int n){
sort(a,a+n,cmp);
fill(used,used+n+1,0);
int end1=0,end2=0;//线程1,2的最晚结束时间
for(int i=0;i<n;++i){
if(used[i]==0){
int*enda=&end1;
int*endb=&end2;
if(end2>end1){
swap(enda,endb);
}
if(*enda<a[i].l){
*enda=a[i].r;
used[i]=1;
}
else{
if(*endb<a[i].l){
*endb=a[i].r;
used[i]=1;
}
}
}
}
ans.clear();
for(int i=0;i<n;++i){//被删除的点
if(used[i]==0){
ans.push_back(a[i].idx);
}
}
sort(ans.begin(),ans.end());
printf("%d\n",ans.size());
if(ans.empty()){
putchar('\n');
}
else{
printf("%d",ans[0]);
for(int i=1;i<ans.size();++i){
printf(" %d",ans[i]);
}
putchar('\n');
}
}
int main()
{
//freopen("/home/lu/Documents/r.txt","r",stdin);
//freopen("/home/lu/Documents/w.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%d%d",&a[i].l,&a[i].r);
a[i].idx=i+1;
}
slove(n);
}
return 0;
}