自己显然不会。。
每次只看3个区间,,然后指针移动的过程是每次都选 右端点最右边的两个区间保存。。
因为这样的区间对后面的影响最大。。
可以和那道 找出出现k次的点的题一起看。。。 第一个博客的。。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=50005;
struct Node{
int l,r;
int id;
}node[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){
return a.r>b.r;
}
bool insect(Node a,Node b,Node c){
return c.l<=b.r&&c.l<=a.r&&b.l<=a.r;
}
int ans[maxn];
int main(){
int n,T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d%d",&node[i].l,&node[i].r);
node[i].id=i;
}
Node tmp[5];
sort(node+1,node+1+n,cmp1);
tmp[1]=node[1];
tmp[2]=node[2];
int tot=0;
for(int i=3;i<=n;++i){
tmp[3]=node[i];
if(insect(tmp[1],tmp[2],tmp[3])){
sort(tmp+1,tmp+4,cmp2);
ans[++tot]=tmp[1].id;
swap(tmp[1],tmp[3]);
}
else{
sort(tmp+1,tmp+4,cmp2);
}
}
printf("%d\n",tot);
sort(ans+1,ans+tot+1);
for(int i=1;i<=tot;++i){
printf("%d%c",ans[i],i==tot?'\n':' ');
}
}
}
本文介绍了一种解决区间覆盖问题的高效算法。该算法通过维护三个活动区间,并始终选择右端点最远的两个区间来更新结果,确保了对后续区间的最大影响。此外,还提供了完整的C++代码实现。
2115

被折叠的 条评论
为什么被折叠?



