题目:POJ-1328
http://poj.org/problem?id=1328
Radar Installation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 102221 | Accepted: 22734 |
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
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.
![](https://i-blog.csdnimg.cn/blog_migrate/f6ffe515205096387436c13c7449b0ed.jpeg)
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
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
解题分析
题目来源:POJ很好很有层次感
核心算法:贪心算法
题目的解法是将每个岛屿需要的雷达安装范围抽象成一个线条,把所有线条按照左端点的先后顺序排序,然后再观察每个左端点:
如果第i个左端点可以覆盖之前所有未覆盖的岛屿,那么观察下一个;
如果第i+1个左端点开始不能覆盖之前的所有未覆盖断电,那么就在i处安装一个雷达
如果i+1可以覆盖,那么观察i+2,以此类推
值得注意的是边界值,y==d的情况和y==0的情况都是需要考虑的
(没错y可以等于0,我因为这个调了2个小时)
Ac码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstring>
#include <math.h>
using namespace std;
int n,d;
struct Island {
double x,y;
double areal,arear;
bool operator < (Island & land) const {
return areal<land.areal;
}
}island[1010];
bool Judge (int first,int last,double pos) {
for (int i=first;i<=last;++i)
if(pos<island[i].areal||pos>island[i].arear) return false;
return true;
}
int main()
{
int time=0;
while (cin>>n>>d) {
time++;
bool flag=0,flagone=0;
int firstNoCover=0;
int rader=0;
if (n==0&&d==0) break;
if (n==1) flagone=1;
for(int i=0;i<n;++i) {
cin>>island[i].x>>island[i].y;
if(island[i].y>d) flag=1;
if(island[i].y<0) flag=1;
if(island[i].y==d){
island[i].areal=island[i].x;
island[i].arear=island[i].x;
}
else {
island[i].areal=island[i].x-sqrt(d*d-island[i].y*island[i].y);
island[i].arear=island[i].x+sqrt(d*d-island[i].y*island[i].y);
}
}
if (flag) {
cout<<"Case "<<time<<": -1"<<endl;
continue;
}
if(flagone) {
cout<<"Case "<<time<<": 1"<<endl;
continue;
}
sort(island,island+n);
for (int i=1;i<n;++i) {
if(i==n-1) {
if(Judge(firstNoCover,i-1,island[i].areal))
{
rader++;
}
else
{
rader+=2;
}
}
else {
if(Judge(firstNoCover,i-1,island[i].areal)) continue;
else {
firstNoCover=i;
rader++;
}
}
}
cout<<"Case "<<time<<": ";
cout<<rader<<endl;
}
return 0;
}