ZOJ地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=360
POJ地址:http://poj.org/problem?id=1328
解题思路:贪心
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <math.h>
5
6 struct P{
7 double x, y, left, right;
8 }p[];
9
int cmp(const void *a, const void *b){
struct P *c = (struct P *)a;
struct P *d = (struct P *)b;
return c->left > d->left ? : -;
}
int main(){
int n, i, j, r, ans, lose, Case = ;
double d, x, dis, high;
while(scanf("%d %d", &n, &r) != EOF){
if(n == && r == ){
break;
}
lose = ;
d = (double)r;
for(i = ; i < n; i++){
scanf("%lf %lf", &p[i].x, &p[i].y);
if(p[i].y > d){ //如果某个点的y左边大于雷达半径,那么一定无法覆盖
lose = ;
}
dis = sqrt(d * d - p[i].y * p[i].y);
p[i].left = p[i].x - dis;
p[i].right = p[i].x + dis;
}
if(lose == ){
printf("Case %d: -1\n", Case++);
continue;
}
qsort(p, n, sizeof(p[]), cmp);
ans = ;
high = p[].right; //令第一个点的右端点为最远点
for(i = ; i < n; i++){
if(p[i].left <= high){
high = p[i].right < high ? p[i].right : high;//如果该点的左端点在最远点内,更新最远点
}
else{ //如果左端点不在最远点内,雷达数+1,更新最远点
ans++;
high = p[i].right;
}
}
printf("Case %d: %d\n", Case++, ans);
}
return ;
53 }