POJ 1328:Radar Installation(贪心)
Radar Installation
Problem 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轴以上是海,以下是岸,海上有许多岛屿,现在要在岸边安装雷达,给出雷达能覆盖的半径,问要覆盖所有岛屿最少需要多少雷达。
解题思路:
(从左到右安防雷达)P为岛,根据每个岛的坐标都能算出其右边与其最远的能覆盖到它的雷达的x坐标:
Code:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=1000+5;
struct Node
{
double x,y;
} a[maxn];
int n,d;
bool cmp(Node a,Node b)
{
double len1= a.x+sqrt(d*d-a.y*a.y);
double len2=b.x+sqrt(d*d-b.y*b.y);
return len1<len2;
}
double dis(Node a,Node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
int ca=1;
while(scanf("%d%d",&n,&d)!=EOF)
{
if(n==0&&d==0)
break;
int flag=1;
for(int i=0; i<n; i++)
{
scanf("%lf%lf",&a[i].x,&a[i].y);
if(a[i].y>d)
flag=0;
}
printf("Case %d: ",ca++);
if(flag==0)
{
printf("-1\n");
continue;
}
sort(a,a+n,cmp);
int cnt=1;
Node last;
last.y=0;
last.x=a[0].x+sqrt(d*d-a[0].y*a[0].y);
for(int i=1; i<n; i++)
{
if(dis(last,a[i])>d)
{
cnt++;
last.x=a[i].x+sqrt(d*d-a[i].y*a[i].y);
}
}
printf("%d\n",cnt);
}
return 0;
}
上一篇: JQuery知识点总结
下一篇: springIOC 基本的注解学习
推荐阅读
-
贪心 E - Radar Installation
-
Radar Installation POJ - 1328
-
【贪心】[POJ1328]Radar Installation
-
POJ 1328:Radar Installation(贪心)
-
POJ1328 Radar Installation(贪心)
-
POJ - 1328 Radar Installation 【贪心】
-
POJ 1328 Radar Installation(贪心)
-
POJ 1328 Radar Installation(贪心)
-
poj1328 Radar Installation
-
POJ1328 Radar Installation