[POJ1328] 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, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.

We use Cartesian coordinate system, defining the coasting is the
x-axis. The sea side is above x-axis, and the land side below. Given the
position of each island in the sea, and given the distance of the
coverage of the radar installation, your task is to write a program to
find the minimal number of radar installations to cover all the islands.
Note that the position of an island is represented by its x-y
coordinates.

Figure A Sample Input of Radar Installations

Input

The
input consists of several test cases. The first line of each case
contains two integers n (1<=n<=1000) and d, where n is the number
of islands in the sea and d is the distance of coverage of the radar
installation. This is followed by n lines each containing two integers
representing the coordinate of the position of each island. Then a blank
line follows to separate the cases.

The input is terminated by a line containing pair of zeros

Output

For
each test case output one line consisting of the test case number
followed by the minimal number of radar installations needed. "-1"
installation means no solution for that case.

Sample Input

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

题解:贪心

按x从小到大排序

首先是要卡边界

对于当前点,如果能被之前的覆盖就直接覆盖

如果不能,那么如果覆盖当前点的圆心(能覆盖到的最右端)的横坐标小于当前圆心横坐标,那么就更新当前圆心坐标,否则ans++

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #define ll long long
 8 using namespace std;
 9
10 const int N = 1010;
11
12 int n,d,ans,tot,flg;
13 double pos;
14
15 struct Node {
16   double x,y;
17   bool operator < (const Node a) const {
18     return x<a.x;
19   }
20 }p[N];
21
22 int gi() {
23   int x=0,o=1; char ch=getchar();
24   while(ch!=‘-‘ && (ch<‘0‘ || ch>‘9‘)) ch=getchar();
25   if(ch==‘-‘) o=-1,ch=getchar();
26   while(ch>=‘0‘ && ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
27   return o*x;
28 }
29
30 int main() {
31   while(scanf("%d%d", &n, &d) && n+d) {
32     ans=1,tot++,flg=0;
33     for(int i=1; i<=n; i++) {
34       p[i].x=gi(),p[i].y=gi();
35     }
36     for(int i=1; i<=n; i++) {
37       if(p[i].y>d) {flg=1;break;}
38     }
39     if(flg) {printf("Case %d: %d\n",tot,-1);continue;}
40     sort(p+1,p+n+1);
41     pos=p[1].x+sqrt(d*d-p[1].y*p[1].y);
42     for(int i=2; i<=n; i++) {
43       if((pos-p[i].x)*(pos-p[i].x)+p[i].y*p[i].y<=d*d) continue;
44       double pos1=p[i].x+sqrt(d*d-p[i].y*p[i].y);
45       if(pos1>pos) ans++;
46       pos=pos1;
47     }
48     printf("Case %d: %d\n",tot,ans);
49   }
50   return 0;
51 }
时间: 2024-10-11 22:03:18

[POJ1328] Radar Installation的相关文章

POJ1328 Radar Installation 【贪心&amp;#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 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

poj1328 Radar Installation(贪心)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1328 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 th

POJ1328 Radar Installation 【贪心&#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 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

POJ1328——Radar Installation

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

poj1328 Radar Installation 区间贪心

题目大意: 在X轴选择尽量少的点作为圆心,作半径为d的圆.使得这些圆能覆盖所有的点. 思路: 把每个点都转化到X轴上.也就是可以覆盖这个点的圆心的位置的范围[a,b].然后按照每个点对应的a从小到大排序.第一点需要特殊处理,我们赋值r=b0 .也就是使得第一个圆的圆心的横坐标尽量大.然后遍历剩下的点.对于i点,如果该点的ai大于r, 就需要增加一个圆,圆心为bi :否则的话,把r更新为r与bi中小的值. 代码: 1 #include<iostream> 2 #include<cstdio

zoj1360/poj1328 Radar Installation(贪心)

对每个岛屿,能覆盖它的雷达位于线段[x-sqrt(d*d-y*y),x+sqrt(d*d+y*y)],那么把每个岛屿对应的线段求出来后,其实就转化成了经典的贪心法案例:区间选点问题.数轴上有n个闭区间[ai,bi],取尽量少的点,使得每个区间内都至少有一个点.选法是:把区间按右端点从小到大排序(右端点相同时按左端点从大到小),然后每个没选的区间选最右边的一个点即可. #include<iostream> #include<cstdio> #include<cstdlib>

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

poj1328(Radar Installation)

题目大意: X轴为陆地,X轴上方为大海,海中有多个小岛,坐标为(x,y).给你任意多个雷达,雷达的扫描范围i是一个以半径为D的圆,问你至少用几个雷达可以将所有小岛覆盖.如不能完全覆盖输出"-1". 解题思路: 简单贪心.以每个小岛为圆心作以半径为D的圆,找出与X轴相交的区间,意思为在这个区间上的任意一点都可以作为圆心,包含这个小岛. 这时就需要把所有小岛的在X轴的区间找到. 有交集的部分说明可以用一个圆覆盖.这是算出最小的圆, 先排序,排序之后找到最左边小岛的的右区间,然后看每一个小岛

Radar Installation(poj1328)(贪心)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54756   Accepted: 12337 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