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[ 1010];
9
10 int cmp( const void *a, const void *b){
11 struct P *c = ( struct P *)a;
12 struct P *d = ( struct P *)b;
13 return c->left > d->left ? 1 : - 1;
14 }
15
16 int main(){
17 int n, i, j, r, ans, lose, Case = 1;
18 double d, x, dis, high;
19 while(scanf( " %d %d ", &n, &r) != EOF){
20 if(n == 0 && r == 0){
21 break;
22 }
23 lose = 0;
24 d = ( double)r;
25 for(i = 0; i < n; i++){
26 scanf( " %lf %lf ", &p[i].x, &p[i].y);
27 if(p[i].y > d){ // 如果某个点的y左边大于雷达半径,那么一定无法覆盖
28 lose = 1;
29 }
30 dis = sqrt(d * d - p[i].y * p[i].y);
31 p[i].left = p[i].x - dis;
32 p[i].right = p[i].x + dis;
33 }
34 if(lose == 1){
35 printf( " Case %d: -1\n ", Case++);
36 continue;
37 }
38 qsort(p, n, sizeof(p[ 0]), cmp);
39 ans = 1;
40 high = p[ 0].right; // 令第一个点的右端点为最远点
41 for(i = 1; i < n; i++){
42 if(p[i].left <= high){
43 high = p[i].right < high ? p[i].right : high; // 如果该点的左端点在最远点内,更新最远点
44 }
45 else{ // 如果左端点不在最远点内,雷达数+1,更新最远点
46 ans++;
47 high = p[i].right;
48 }
49 }
50 printf( " Case %d: %d\n ", Case++, ans);
51 }
52 return 0;
2 #include < string.h>
3 #include <stdlib.h>
4 #include <math.h>
5
6 struct P{
7 double x, y, left, right;
8 }p[ 1010];
9
10 int cmp( const void *a, const void *b){
11 struct P *c = ( struct P *)a;
12 struct P *d = ( struct P *)b;
13 return c->left > d->left ? 1 : - 1;
14 }
15
16 int main(){
17 int n, i, j, r, ans, lose, Case = 1;
18 double d, x, dis, high;
19 while(scanf( " %d %d ", &n, &r) != EOF){
20 if(n == 0 && r == 0){
21 break;
22 }
23 lose = 0;
24 d = ( double)r;
25 for(i = 0; i < n; i++){
26 scanf( " %lf %lf ", &p[i].x, &p[i].y);
27 if(p[i].y > d){ // 如果某个点的y左边大于雷达半径,那么一定无法覆盖
28 lose = 1;
29 }
30 dis = sqrt(d * d - p[i].y * p[i].y);
31 p[i].left = p[i].x - dis;
32 p[i].right = p[i].x + dis;
33 }
34 if(lose == 1){
35 printf( " Case %d: -1\n ", Case++);
36 continue;
37 }
38 qsort(p, n, sizeof(p[ 0]), cmp);
39 ans = 1;
40 high = p[ 0].right; // 令第一个点的右端点为最远点
41 for(i = 1; i < n; i++){
42 if(p[i].left <= high){
43 high = p[i].right < high ? p[i].right : high; // 如果该点的左端点在最远点内,更新最远点
44 }
45 else{ // 如果左端点不在最远点内,雷达数+1,更新最远点
46 ans++;
47 high = p[i].right;
48 }
49 }
50 printf( " Case %d: %d\n ", Case++, ans);
51 }
52 return 0;
53 }