题目意思:
给一些闭区间,求最少需要多少点,使得每个区间至少一个点。
?
?
-
样例输入
4 1 5 2 4 1 4 2 3 3 1 2 3 4 5 6 1 2 2
样例输出1 3 1
题目分体:?
区间贪心,我认为区间上的贪心算法,最主要的还是排序的方式,只要排序的方式合理,就能很好的使用贪心,贪心的本质虽然是选择当前最优的解,作为全军最优解的一部分,如果排序不当回造成好的条件选择。本题只给出的排序的方式,相信大家都会使用贪心的。
?
AC代码:
#include
#include
using namespace std;
struct node{
int r,l;
}a[101];
int cmp(node a1,node b1){//按右端点从大到小,左端点从大到小定义排序方式。
if(a1.relse if(a1.r==b1.r&&a1.l>b1.l) return 1;
return 0;
}
int main()
{
int n;
while(cin>>n){
int x,y;
for(int i=0;icin>>x>>y;
a[i].l=xa[i].r=x>y?x:y;
}
sort(a,a+n,cmp);
int k=1;
node p=a[0];
for(int i=1;iif(p.r p=a[i]; k++;
}
}
cout<}
return 0;
}
?