跳到正文

个人记录

区间贪心

AC_Snake约 11 分钟
原文
文章信息与版本

文章信息

发布
2025年8月10日
收录
2025年12月12日
更新
2026年8月17日
篇幅
约 11 分钟

版本

  1. v1当前

概念

区间贪心是一种用贪心的思想解决区间问题的策略,通过以左端点或右端点排序,每次选此刻局部最优的区间来构成全局的最优解。

模型

区间贪心有一些模型,大多的题目都是按这些模型出的,所以只要熟练这些模型,那些题目就能比较轻松的通过。

模型 1 :点覆盖区间问题 ybt 1324 整数区间

  1. 题意:用最少的点,让每个区间至少有一个点属于该区间。
  2. 思路: 这道题我们可以先以右端点从小到大排序,这时就有两种情况:
    1. 下一个区间的左端点在现在选的这个点位置的左边,下一区间就也被这个点覆盖了;
    2. 左端点在现在选的这个点位置的右边,说明两个区间是互不干预、完全独立的,无法通过一个点把这两个区间完全覆盖,所以等会再来一个点将其覆盖即可。
  3. 时空复杂度:Time: O(nlog⁡nn \log n); Memory: O(n);

代码:

C++
#include<bits/stdc++.h>
using namespace std;

int const N=1e4+10;
int n,ans,pos=-1;

struct line{
	int l,r;
}an[N];

bool cmp(line a,line b){
	return a.r<b.r;
}

int main(){
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>an[i].l>>an[i].r;
	sort(an+1,an+n+1,cmp);
	for(int i=1;i<=n;i++){
		if(pos<an[i].l){
			ans++;
			pos=an[i].r;
		}
	}
	cout<<ans;
	return 0;
}

模型 2 :点和区间的配对问题 P2887 Sunscreen G

  1. 题意:有若干个区间和若干个点,如果点在区间范围之内,他们就能配对,问最多有多少对能配对的点和区间。
  2. 思路:我们可以按区间的右端点从小到大排序,点也是按从小到大排序,然后枚举每个区间,再看看有没有点能跟它配对,有的话,配对数加一,点数量减一,接着立即 break ;否则不管。
  3. 时空复杂度:Time: O(n2log⁡nn^2 \log n); Memory: O(n);

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long

int const N=25e2+10;
int n,m,ans;

struct SPF{
	int x,siz;
}spf[N];

struct line{
	int l,r;
}an[N];

bool cmp1(SPF a,SPF b){
	return a.x<b.x;
}

bool cmp2(line a,line b){
	return a.r<b.r;
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)	cin>>an[i].l>>an[i].r;
	for(int i=1;i<=m;i++)	cin>>spf[i].x>>spf[i].siz;
	sort(an+1,an+n+1,cmp2);
	sort(spf+1,spf+m+1,cmp1);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(an[i].l<=spf[j].x && an[i].r>=spf[j].x && spf[j].siz){
				spf[j].siz--,ans++;
				break;
			}
		}
	}
	cout<<ans;
	return 0;
}

模型 3 :最大不相交区间数量 D4326 活动选择

  1. 题意:有若干个区间,要选出尽量多的区间,但不能互相重叠,问能选多少个区间。
  2. 思路:让右端点从小到大排序,因为早结束早开始选下一个区间,如果开始时间早于上一个区间的右端点,就不行;否则接下一个。
  3. Time: O(nlog⁡nn \log n); Memory: O(n);

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long

int const N=25e2+10;
int n,lst,ans;

struct line{
	int l,r;
}an[N];

bool cmp(line a,line b){
	return a.r<b.r;
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>an[i].l>>an[i].r;
	sort(an+1,an+n+1,cmp);
	for(int i=1;i<=n;i++){
		if(lst<=an[i].l)	lst=an[i].r,ans++;
	}
	cout<<ans;
	return 0;
}

模型 4 :区间覆盖 P1668 Cleaning Shifts S

  1. 题意:有若干个区间,问能否选出一些区间覆盖一个大区间,能就输出区间数;否则输出 -1 。
  2. 思路:这道题我们可以按左端点从小到大排序,然后遍历区间,维护当前合并区间的右端点,如果此时这个区间能接上上一个区间,接上,更新右端点;否则,输出 -1 。
  3. 时空复杂度:Time: O(n2log⁡nn^2 \log n); Memory: O(n);

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long

int const N=25e3+10;
int n,tim,ans=1;

struct line{
	int l,r;
}an[N];

bool cmp(line a,line b){
	if(a.l==b.l)	return a.r>b.r;
	return a.l<b.l;
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>tim;
	for(int i=1;i<=n;i++)	cin>>an[i].l>>an[i].r;
	sort(an+1,an+n+1,cmp);
	if(an[1].l>1){
		cout<<-1;
		return 0;
	}
	int R=an[1].r+1;
	for(int i=2;i<=n;i++){
		if(an[i].l>R){
			cout<<-1;
			return 0;
		}
		if(an[i].r<R)	continue;
		int mx=0;
		for(int j=i;j<=n;j++){
			if(an[j].l>R)	break;
			if(an[j].r>mx)	mx=an[j].r;
		}
		if(mx>=tim){
			cout<<ans+1;
			return 0;
		}
		R=mx+1,ans++;
	}
	if(R<=tim)	cout<<-1;
	else	cout<<ans;
	return 0;
} 

模型 5 :可相交区间分组 P10793 Double Champions

  1. 题意: 有若干个区间,现在要把它们分成若干个组,使得每一组区间的交的总长度都 ≥ w,如果没有任何方案满足条件,输出 No。
  2. 思路:我们发现:组内区间越多,交集就越小。所以如果某个区间本身就 < w ,则无解。判断有解后,先按区间左端点从小到大排序,然后遍历每一个区间,维护一个交集的 L 和 R 。L 和 R 都会越来越小。当区间交集长度 < w 时,就在开一组放。
  3. 时空复杂度:Time: O(Tnlog⁡nT n \log n); Memory: O(n);

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long 

int const N=2e5+10;
ll T,n,m,ans;

struct line{
	ll l,r;
}an[N];

bool cmp(line a,line b){
	if(a.l==b.l)	return a.r<b.r;
	return a.l<b.l; 
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--){
		cin>>n>>m;
		bool flag=0;
		for(int i=1;i<=n;i++){
			cin>>an[i].l>>an[i].r;
			if(an[i].r-an[i].l+1<m)	flag=1;
		}
		if(flag){
			cout<<"No\n";
			continue;
		}
		if(!m){
			cout<<"1\n";
			continue;
		}
		sort(an+1,an+n+1,cmp);
		ll L=an[1].l,R=an[1].r,ans=0;
		ans++;
		for(int i=2;i<=n;i++){
			ll tl=max(an[i].l,L),tr=min(an[i].r,R);
			if(tr-tl+1<m)	ans++,L=an[i].l,R=an[i].r;
			else	L=tl,R=tr;
		}
		cout<<ans<<"\n";
	}
	return 0;
}

模型 6 :不相交分组 P2859 Stall Reservations S

  1. 题意:有若干个区间,现在要把它们分成若干个组,使得每一组都互不重叠,输出最小组数和每个区间的小组编号。
  2. 思路:先按区间左端点从小到大排序,用小顶堆维护全部组里最小的右端点,并记录那个组的编号,然后枚举剩下区间,如果区间左端点都小于等于组里最小的右端点,就新开一组;否则加入那一组。
  3. 时空复杂度:Time: O(nlog⁡nn \log n); Memory: O(n);

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long 

int const N=5e4+10;
ll n,cnt,ans[N];

struct str{
	ll r,id;
	bool operator < (const str & tmp) const {
		return r>tmp.r;
	}
};

priority_queue<str> pq;

struct line{
	ll l,r,id;
}an[N];

bool cmp(line a,line b){
	return a.l<b.l;
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>an[i].l>>an[i].r;
		an[i].id=i;
	}
	sort(an+1,an+n+1,cmp);
	pq.push({an[1].r,1}),ans[an[1].id]=1;
	cnt++;
	for(int i=2;i<=n;i++){
		ll R=pq.top().r,tot=pq.top().id;
		if(an[i].l<=R)	cnt++,pq.push({an[i].r,cnt}),ans[an[i].id]=cnt;
		else{
			pq.pop();
			pq.push({an[i].r,tot}),ans[an[i].id]=tot;
		}
	}
	cout<<pq.size()<<"\n";
	for(int i=1;i<=n;i++)	cout<<ans[i]<<"\n";
	return 0;
} 
原文