您的位置:首页 > 其它

POJ 1328

2015-11-15 16:08 176 查看
Radar Installation

Time Limit: 1000MSMemory Limit: 10000K
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轴的上方是海洋,x的下方是陆地,在海洋中有n个小岛,我们需要雷达来监控小岛,且雷达只能放在x轴上,给定的d就是雷达的覆盖范围,
如今给点我们n个小岛的坐标,要我们求出需要覆盖所有的小岛所需的最少雷达数。
思路:我们可以以每个点为圆心,然后以d为半径,求出圆与x轴的交点,则有两个点满足条件,可以放置雷达,但我们需要求出最少的雷达数,所以我们需要把每个点求出的那两个
可以放置雷达的点按照横坐标从小到大排序,以第一个点设置为雷达起始点,如果该点的下一个点的左端位置要比该点的右端还要大,那么雷达肯定覆盖不到下一个点,那么我们就需要雷达数加1,然后重新设置雷达的位置,
如果该点的右边比雷达的右边还要小,那么肯定能够被覆盖,但是因为我们要计算雷达的最小数目,所以我们需要重新设定雷达的位置了,如果所有的情况都不能找到雷达能够覆盖点,那么
我们需要输出-1.
这是百度的题解:题解:排序完成后,第一个雷达建立在区间的右端,而后一次判断每个区间的左端点,如果在最新建立的雷达右面,那么肯定需要一个雷达,而且也建在区
间右端。如果左端点在雷达左面,这个时候要考虑区间的右端在雷达的左面还是右面,如果是右面,那雷达位置就不变,如果在左面,那现在的雷达是覆盖不了的,所以要把
雷达放在该区间的右端点!因为这样同时还能覆盖原来的岛,还能覆盖现在的岛。//也许比我解释的更加清楚一些
附上详细的代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <iomanip>
#include <cmath>
using namespace std;
struct node{
double left,rigth;//我们用来记录每个点求出的雷达点
}num[1005];
bool cmp(node a,node b)
{
return a.left<=b.left;//每个雷达点按照横坐标从小到大排序
}
int main()
{
int x,y,n,d,flag,cnt,i,k=1;
double p;
while(cin>>n>>d)
{
if(n==0) break;
if(d<=0) d=0;//如果半径小于0,肯定是不可能出现的,那么我们就把它设定为0
flag=1;
for(i=0;i<n;i++)
{
cin>>x>>y;
if(d*d-y*y<0)
{
flag=0;//如果该点的纵坐标比半径还大,那么雷达覆盖不到,设定为0
continue;
}
num[i].left=x-sqrt(d*d-y*y);
num[i].rigth=x+sqrt(d*d-y*y);
}
if(flag)//如果能够找到能够覆盖点的雷达
{
sort(num,num+n,cmp);
cnt=1;
p=num[0].rigth;
for(i=1;i<n;i++)
{
if(num[i].left>p)
{
p=num[i].rigth;//如果该点的左坐标比雷达的右坐标都大,那么雷达肯定覆盖不到,我们就把雷达数加1,并把该点的右坐标设定为雷达
cnt++;
}else if(num[i].rigth<p)//如果该点能够被雷达覆盖,但是我们要求出最少的雷达数,我们需要使雷达尽可能的多覆盖,所以要把雷达的位置缩小
{
p=num[i].rigth;
}
}
cout<<"Case "<<k++<<": "<<cnt<<endl;
}else{//找不到能够覆盖点的雷达
cout<<"Case "<<k++<<": "<<"-1"<<endl;
}
//cout<<"Case "<<k++<<": "<<cnt<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: