贪心 | POJ1328.Radar Installation
link: 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
Source
题意
有一条海岸线,它以上是海和岛屿(岛屿看做质点), 以下是陆地,给出所有岛屿的坐标和雷达的探测范围d,问至少需要多少雷达才能覆盖将所有的岛屿。
思路
以每个岛屿为圆心,d为半径画圆,与x轴的交点即是雷达的安装位置的区间;
n个岛屿则有n个区间,问题转化为在同轴n个区间上 找到最少的点数 使所有区间都至少包含一个点;
将区间按照右端点递增排序,将每个区间的所有右边区间判定是否存在想重叠部分(双重循环),如果有,则删除,否则保留、遍历完所有区间后得到最少的点数。
注意
区间的起始位置均为 double类型, 刚开始随手写成int类型,WA,于是debug了好久。
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 1000;
struct island
{
int x;
int y;
};
island isl[maxn];
struct interval
{
double s;
double e;
};
interval itv[maxn];
bool cmp( interval a, interval b )
{
return a.e < b.e;
}
bool visit[maxn] = {false};
int main()
{
int n, d, i, j;
bool flag = true;
int time = 1;
double len;
int ans = 0;
//输入不确定组数的样例
while( cin >> n >> d && n != 0)
{
flag = true;
//validity 1. d<0; 2. y>d
if( d < 0 )
flag = false;
//input data
for( i = 0; i < n; i++ )
{
cin >> isl[i].x >> isl[i].y;
// validity
if( isl[i].y > d )
flag = false;
}
//output #1 invalidity
cout << "Case " << time++ << ": ";
if( !flag )
{
cout << "-1" << endl;
continue;
}
//get all interval
for( i = 0; i < n; i++ )
{
len = sqrt( double(d*d)- double(isl[i].y * isl[i].y) );
itv[i].s = isl[i].x - len;
itv[i].e = isl[i].x + len;
}
sort( itv, itv + n, cmp );
memset( visit, false, sizeof(visit) );
ans = 0;
for( i = 0; i < n; i++ )
{
if( !visit[i] )
{
visit[i] = true;
for( j = i; j < n; j++ )
{
if( !visit[j] && itv[j].s <= itv[i].e )
visit[j] = true;
}
ans++;
}
}
cout << ans << endl;
}
return 0;
}