poj 1328Radar Installation

//贪心题,所谓贪心只是一个思想,没有特定的解法,只是单纯地找出当前最多的解,能多就多,最后企图达到整体的最优,这就是贪心。

//这道题是说有一个x轴作为海岸分界线,上方是海,有一个个的海岛,下方是陆地,有一个雷达,扫描范围是半径为d的圆,只能安装在x轴上,问最少要安装多少个雷达?

//如图,首先是只要明确A和B点有什么意义,这道题就能很快解答,以A和B点做圆心,d为半径就刚好交C点,所以A到B是一个区间范围,在[A,B]上建圆就可以把C给概括起来(易证),然后整道题目就变成了区间包围问题;把每个点的这两个左右点都算出来,按左点进行排序,然后更新左右区间,见代码(l和r),只要两个区间有公共范围,就可以用一个雷达把他们都罩起来;

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 struct point{
 8     double left;
 9     double right;
10 }p[1010];
11
12 bool cmp(point a, point b){
13     return (a.left < b.left);
14 }
15
16 int main()
17 {
18     int n, d, res, tcase = 0;
19     while(cin >> n >> d && (n || d)){
20         bool flag = false;
21         for(int i = 0; i < n; i++){
22             double x, y;
23             scanf("%lf%lf", &x, &y);
24             if(y > d)
25                 flag = true;
26             else{
27                 p[i].left = x-sqrt(d*d-y*y);
28                 p[i].right = x+sqrt(d*d-y*y);
29             }
30         }
31         printf("Case %d: ", ++tcase);
32         if(flag)
33             printf("-1\n");
34         else{
35             sort(p,p+n,cmp);
36             res = 1;
37             double l = p[0].left, r = p[0].right;
38             for(int i = 1; i < n; i++){
39                 if(r < p[i].left){
40                     res++;
41                     r = p[i].right;
42                 }
43                 else if(r > p[i].right)
44                     r = p[i].right;
45                 l = p[i].left;
46             }
47             printf("%d\n", res);
48         }
49     }
50     return 0;
51 }

时间: 2024-08-05 05:40:42

poj 1328Radar Installation的相关文章

POJ 1328、Radar Installation 贪心

jQuery的属性操作非常简单,下面以一个a元素来说明属性的获取/设置/删除操作 <body> <a>jquery.com</a> </body> 添加属性 $('a').attr('href', 'http://www.jquery.com') 添加多个属性 $('a').attr({'href':'http://www.jquery.com', 'title':'jquery.com'}) 获取属性 $('a').attr('href') class属性

[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

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50843   Accepted: 11415 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 贪心题解

本题是贪心法题解,不过需要自己观察出规律,这就不容易了,很容易出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 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

Poj 1328 / OpenJudge 1328 Radar Installation

1.Link: http://poj.org/problem?id=1328 http://bailian.openjudge.cn/practice/1328/ 2.Content: Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52833   Accepted: 11891 Description Assume the coasting is an infinite straig

[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

http://poj.org/problem?id=1328 //第一次:从左边第一个未被覆盖的island开始 -->>失败 因为还有y坐标这一因素 不能保证贪心//第二次:找两个点 确定一个圆 ----->>>其实早就应该发现错误 漏洞百出 不具有普遍性//从左边第一个未覆盖的点作为基点 找到第一个 y坐标>=的点(如果没有找到) 做这两个点的公共圆//如果不能做这两个点的公共圆 或者 没有y>=的点 那么做这个圆的右极限圆//更新覆盖的点//蠢-->&