您的位置:首页 > 其它

ZOJ-1360 || POJ-1328——Radar Installation

2014-11-04 11:35 288 查看
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
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 ? 1 : -1;
}

int main(){
int n, i, j, r, ans, lose, Case = 1;
double d, x, dis, high;
while(scanf("%d %d", &n, &r) != EOF){
if(n == 0 && r == 0){
break;
}
lose = 0;
d = (double)r;
for(i = 0; i < n; i++){
scanf("%lf %lf", &p[i].x, &p[i].y);
if(p[i].y > d){ //如果某个点的y左边大于雷达半径,那么一定无法覆盖
lose = 1;
}
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 == 1){
printf("Case %d: -1\n", Case++);
continue;
}
qsort(p, n, sizeof(p[0]), cmp);
ans = 1;
high = p[0].right; //令第一个点的右端点为最远点
for(i = 1; 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 0;53 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: