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

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;

53 }

时间: 2024-08-29 07:27:22

ZOJ-1360 || POJ-1328——Radar Installation的相关文章

[2016-02-04][POJ][1328][Radar Installation]

[2016-02-04][POJ][1328][Radar Installation] Radar Installation Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit Status Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in

POJ 1328 Radar Installation 贪心题解

本题是贪心法题解,不过需要自己观察出规律,这就不容易了,很容易出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 按照x轴大小排序 2 从最左边的点循环,首先找到最小x轴的圆 3 以这个圆判断可以包括右边的多少个圆,直到不可以包括下一个点,那么继续第2步,画一个新圆. 看代码吧,应该很清晰直观的了. 效率是O(n),虽然有嵌套循环,但是下标没有重复,一遍循环就可以了,故此是O(n). #include <stdio.h> #include <cmath> #incl

Poj 1328 Radar Installation 贪心

题目链接:http://poj.org/problem?id=1328 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52768   Accepted: 11867 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the othe

[ACM] POJ 1328 Radar Installation (贪心,区间选点问题)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 51131   Accepted: 11481 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

POJ 1328 Radar Installation 雷达安装 贪心问题求解

题目链接: POJ 1328 Radar Installation Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coas

poj 1328 Radar Installation(贪心+快排)

Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, s

poj 1328 Radar Installation (贪心)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52823   Accepted: 11883 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

poj 1328 Radar Installation 【贪心】【区间选点问题】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54798   Accepted: 12352 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

POJ 1328 Radar Installation(经典贪婪)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54143   Accepted: 12178 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

POJ 1328 Radar Installation 【贪心 区间选点】

解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的位置在x轴上,所以我们设雷达的坐标为(x,0),对于任意一个岛屿p(a,b),因为岛屿要满足在雷达的覆盖范围内,所以 (x-a)^2+b^2=r^2,解得 xmin=a-sqrt(r*r-b*b);//即为区间的左端点 xmax=a+sqrt(r*r-b*b);//即为区间的右端点 接下来区间选点即