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 }
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 }
相关文章推荐
- zoj 1360 || poj 1328 Radar Installation
- ZOJ-1360(POJ-1328) Radar Installation
- zoj 1259 || poj 1363 Rails(栈~!)
- zoj 1986 || poj 1631 Bridging Signals(最长上升子序列N*logN)
- poj1328
- poj 2029 && zoj 1716 Get Many Persimmon Trees
- Radar Installation(poj 1328)
- zoj 1700 || poj 1577 Falling Leaves(BST应用)
- poj1328 Radar Installation —— 贪心
- poj-1328
- 【有根树的同构】ZOJ 1990/POJ 1635
- ZOJ-1025(POJ-1065、HDU-1051) Wooden Sticks
- zoj 1700 || poj 1577 Falling Leaves(BST)
- zoj 2870 || poj 3304 Segments
- POJ 1458 Common Subsequence (zoj 1733 ) LCS
- POJ 1325、ZOJ 1364、HDU 1150 Machine Schedule - from lanshui_Yang
- POJ 1328 Radar Installation 题解
- zoj 1525 || poj 1422 Air Raid
- 【POJ】1328 - Radar Installation
- poj 1328 【Radar Installation】【几何转化、区间覆盖】