题目连接:点击打开链接
题目思路:这是一道贪心中的区间取点问题,就是取最少的点使得每一个区间中都至少含有一个点。
思路转换:原题是不直接说是区间选点的,我们以每一个岛屿为圆心,雷达半径作为半径画一个圆必定与x轴相交(如果不j交就直接输出-1),那么有两个交点(可能是一个,但没影响),我们就把这两个端点看做是这个区间的端点,那么就有了一个区间,很多岛屿便能画出很多的区间出来,这便成了区间选点问题了
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
const int MAX=1001;
struct point{
double left,right;
};
bool order_by_right(struct point a,struct point b){ //按照区间的右端点从小到大排序,如果右端点相等,那么按照左端点从大到小排序
if(a.right!=b.right)
return a.right<b.right;
else
return a.left>b.left;
}
int main()
{
struct point a[MAX];
int i,j,radous,island,count,times=0,temp_x,temp_y;
while(~scanf("%d%d",&island,&radous)&&island+radous!=0){
count=1;
times++;
for(i=0;i<island;i++){
scanf("%d%d",&temp_x,&temp_y);
if(temp_y>radous){
printf("-1");
continue;
}
else{
double temp=sqrt(radous*radous-temp_y*temp_y);
a[i].left=temp_x-temp; //算的每一个区间的左右端点
a[i].right=temp_x+temp;
}
}
sort(a,a+island,order_by_right);
double end=a[0].right;
for(i=1;i<island;i++){
if(a[i].left>end){
end=a[i].right; //如果后面的区间的左端点比上一次的(最后被选中最为比较标准)的区间的右端点小,那么就重新选一个标准区间的右端点作为标准
count++;
}
}
printf("Case %d: %d\n",times,count);
}
return 0;
}