凌乱的YYY

QAQ
区间覆盖问题之最大不相交覆盖问题
这个问题可以用DP解决,也可以用贪心解决。
这里给出贪心做法。
把终点升序排列,模拟判断即可。

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
struct node{
	int s,t;
}a[1100000];
bool comp(node x,node y)
{
	if(x.t==y.t) return x.s<y.s;
	return x.t<y.t;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	 scanf("%d%d",&a[i].s,&a[i].t);
	
	sort(a+1,a+n+1,comp);
	int end=-1;
	int ans=0;
	for(int i=1;i<=n;i++)
	 if(a[i].s>=end)
	 {
	 	ans++;
	 	end=a[i].t;
	 }
	printf("%d",ans);
}
原文地址:https://www.cnblogs.com/ht008/p/7737459.html