POJ 1328 Radar Installation

http://poj.org/problem?id=1328

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

//最终策略:把注意力全部集中在x轴上 , 对每个island 作为圆心 交在x轴上有一个区间 那么radar在这个区间内都可以覆盖这个岛屿
//所以讲所有岛屿的左区间进行排序 区间重叠的共用一个radar
//思路一错 全盘否定

网上题解盗一张图说明

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdio.h>
 4 #include <algorithm>
 5 #include <math.h>
 6 using namespace std;
 7
 8 struct Island
 9 {
10     int x,y;
11     double limit_l, limit_r;
12     bool cover;
13     bool operator < (Island a) const
14     {
15         return limit_l < a.limit_l;//按左区间排序
16     }
17 }island[1024];
18 int N, d;
19
20 int solve()
21 {
22     double limit_l, limit_r;
23     int cnt = 1;
24     if (!N) return 0;//如果N==0
25     for (int i = 0; i < N; i++)
26     {
27         island[i].limit_l = island[i].x - sqrt( d*d - island[i].y*island[i].y );
28         island[i].limit_r = island[i].x + sqrt( d*d - island[i].y*island[i].y );
29     }
30     sort(island, island+N);//按照左区间排序
31     limit_l = island[0].limit_l;//这里最后忘了初始化 然后挂了
32     limit_r = island[0].limit_r;
33     for(int  i = 1; i < N; i++)
34     {
35         //cnt++;//第一个雷达
36         if (island[i].limit_l > limit_r)
37         {
38             cnt++;
39             limit_l = island[i].limit_l;
40             limit_r = island[i].limit_r;
41         }
42         else if (island[i].limit_r < limit_r)
43         {
44             limit_r = island[i].limit_risland[i].x - sqrt( d*d island[i].y*island[i].y );
45         }
46     }
47     return cnt;
48 }
49 int main()
50 {
51     freopen("in.txt", "r", stdin);
52     bool fail = false;
53     int Case = 0;
54     while (~scanf("%d%d",&N, &d))
55     {
56         if (N == 0 && d == 0) break;
57         getchar();//空行
58         fail = false;//失败的标志
59         Case++;
60         for (int i = 0;i < N; i++)
61         {
62             scanf("%d%d", &island[i].x, &island[i].y);
63             if ( island[i].y > d || island[i].y < 0)
64             {
65                 fail = true;
66             }
67         }
68         if (fail) printf("Case %d: %d\n",Case, -1);
69         else   printf("Case %d: %d\n", Case, solve());
70     }
71     return 0;
72 }

时间: 2024-10-04 17:33:40

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);//即为区间的右端点 接下来区间选点即