首页 > 代码库 > Radar Installation(贪心)
Radar Installation(贪心)
Radar Installation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 54295 | Accepted: 12208 |
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.
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
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 0Sample Output
Case 1: 2 Case 2: 1题意:有一个坐标轴,在x轴的上方时海,下方是陆地,x轴为海岸线,海上有n个小岛,因为种种原因要在海岸线上装雷达,每个雷达的覆盖范围是以r为半径的圆,请用最少的雷达数覆盖所有的小岛,当无法完全覆盖时输出-1;思路:典型的贪心思想。以小岛为圆心以r为半径做园,计算出圆与x轴的左交点和右交点。左交点为x-sqrt(r*r-y*y),右交点为x+sqrt(r*r+y*y);然后对左交点从小到大排序,令初始雷达为最小岛屿的右交点,如果i点的左交点在雷达的右面,则需要重新装一个雷达,然后令当前点为新雷达的右交点,否则如果i点的右交点在当前雷达的左边,则把当前雷达的位置更新为该点的右交点。#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; struct node { double l,r; }point[1010]; int cmp(struct node a,struct node b)//对左交点进行排序; { return a.l<b.l; } int main() { int n,r,i; double x,y,t;//用t来储存当前雷达的位置 int cnt=1; while(~scanf("%d %d",&n,&r)) { if(n==0&&r==0) break; int ans=1;//记录安装雷达的个数 for(i=0;i<n;i++) { scanf("%lf %lf",&x,&y); point[i].l=x-sqrt(r*r-y*y);//当前点与x轴的左交点 point[i].r=x+sqrt(r*r-y*y);//当前点与x轴的右交点 if(y>r||y<0||r<=0)//列举出三种无法覆盖的情况 ans=-1; } sort(point,point+n,cmp);//排序 t=point[0].r;//令初始值为位置最小岛屿的右交点 for(i=1;i<n&&ans!=-1;i++) { if(point[i].l>t) { ans++; t=point[i].r; } if(point[i].r<t) { t=point[i].r; } } printf("Case %d: %d\n",cnt++,ans); } return 0; }
Radar Installation(贪心)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。