POJ 1328:Radar Installation(贪心)
2017-08-01 20:39
344 查看
Radar Installation
Time limit:1000 ms Memory limit:10000 kB OS:LinuxProblem 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 21 2
-3 1
2 1
1 2
0 2
0 0
Sample Output
Case 1: 2Case 2: 1
题意:
X轴以上是海,以下是岸,海上有许多岛屿,现在要在岸边安装雷达,给出雷达能覆盖的半径,问要覆盖所有岛屿最少需要多少雷达。解题思路:
(从左到右安防雷达)P为岛,根据每个岛的坐标都能算出其右边与其最远的能覆盖到它的雷达的x坐标:x=xp+R2−y2p−−−−−−√根据这个x坐标来贪心,如果上一个雷达能覆盖到当前的岛,继续使用上一个雷达,如果不能,就用上面的公式计算出一个新的雷达坐标,总数+1。
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; }
相关文章推荐
- poj 1328 Radar Installation (简单的贪心)
- (一般)POJ-1328 区间贪心,几何
- 贪心问题:区间覆盖 POJ 1328 Rader Installation
- 【贪心专题】POJ 1328 G - Radar Installation (区间覆盖)
- POJ 1328 Radar Installation 贪心
- POJ1328 贪心
- Radar Installation POJ - 1328 贪心
- poj 1328 Radar Installation (贪心)
- poj 1328 Radar Installation(贪心,线段重叠)
- poj1328 区间贪心 雷达覆盖岛屿
- POJ 1328 Radar Installation(区间贪心)
- POJ 1328(排序+贪心)
- poj 1328 Radar Installation 排序贪心
- POJ:1328 Radar Installation (贪心)
- POJ 1328 Radar Installation(贪心)
- POJ1328 Radar Installation (区间选点问题,贪心)
- POJ 1328 Radar Installation【贪心】
- POJ 1328 Radar Installation 【贪心 区间选点】
- poj 1328 贪心 转化
- poj 1328 Radar Installation (逆思维+贪心)