POJ 1328 Radar Installation(贪心)

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

题意:

假设滑行是无限直线。土地在海岸的一边,海在另一边。每个小岛是位于海边的一个点。位于海岸上的任何雷达装置只能覆盖距离,所以如果两者之间的距离最大为d,则海中的岛屿可以被半径装置覆盖。 
我们使用笛卡尔坐标系,定义惯性是x轴。海侧在x轴之上,陆侧在下。考虑到每个岛屿在海洋中的位置,并且考虑到雷达装置的覆盖距离,您的任务是编写一个程序,以找到覆盖所有岛屿的最少数量的雷达装置。注意,岛的位置由其xy坐标表示。

思路:

由于雷达只能处于x轴位置,所以对于每个雷达,我们都可以计算出能覆盖它的雷达的最大区间,即最小最坐标和最大右坐标。

接下来是贪心,按照右坐标从小到大排序,只要区间有交集,就可以用一个雷达来覆盖。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<math.h>
 5 using namespace std;
 6
 7 const int maxn = 1000 + 5;
 8
 9 int d, n;
10 int vis[maxn];
11
12 struct node
13 {
14     double left, right;
15 }a[maxn];
16
17 bool cmp(node a, node b)
18 {
19     return a.right < b.right ;
20 }
21
22 double cacl(int y)
23 {
24     return sqrt((double)(d*d - y*y));
25 }
26
27 int main()
28 {
29     //freopen("D:\\txt.txt", "r", stdin);
30     int x, y, kase = 0;
31     while (cin >> n >> d && n)
32     {
33         memset(vis, 0, sizeof(vis));
34         int ok = 0;
35         for (int i = 0; i < n; i++)
36         {
37             cin >> x >> y;
38             if (y > d)   {ok = 1;}   //如果y>d,肯定覆盖不着
39             if (!ok)
40             {
41                 double ra = cacl(y);
42                 //雷达的左右区域
43                 a[i].left = x - ra;
44                 a[i].right = x + ra;
45             }
46         }
47         if (!ok)
48         {
49             sort(a, a + n, cmp);
50             int cnt = 0;
51             for (int i = 0; i < n; i++)
52             {
53                 if (!vis[i])
54                 {
55                     vis[i] = 1;
56                     for (int j = 0; j < n; j++)
57                     {
58                         if (!vis[j] && a[j].left <= a[i].right)
59                             vis[j] = 1;
60                     }
61                     cnt++;
62                 }
63             }
64             cout << "Case " << ++kase << ": " << cnt << endl;
65         }
66         else  cout << "Case "<<++kase << ": -1" << endl;
67     }
68     return 0;
69 }
时间: 2024-11-02 00:32:38

POJ 1328 Radar Installation(贪心)的相关文章

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(贪心+快排)

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 贪心算法

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 贪心 难度:1

http://poj.org/problem?id=1328 思路: 1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解 2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆心允许范围,问题转化为在许多圆心允许范围内取尽可能少的点,也即在许多线段上取尽可能少的点,使得所有线段上都有点被取到 3.从左往右考虑,完全在左边的线段肯定要取点,如果这个点在当前线段上已经取了,明显就可以忽略当前线段,明显在线段上的最优点是右端点 1 #include <iostream> 2

POJ 1328 Radar Installation#贪心(坐标几何题)

(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> using namespace std; struct node { double l,r; //找到以岛为圆心,以d为半径的圆与坐标x轴的左交点l.右交点r //雷达只有设在l~r之间,岛才在雷达覆盖范围内 }a[1005]; int cmp(node a,node b) { return a.l

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

[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: 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