标签:
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<tex2html_verbatim_mark> distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d<tex2html_verbatim_mark> .
We use Cartesian coordinate system, defining the coasting is the x<tex2html_verbatim_mark> -axis. The sea side is above x<tex2html_verbatim_mark> -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<tex2html_verbatim_mark> - y<tex2html_verbatim_mark>coordinates.
Input
The input consists of several test cases. The first line of each case contains two integers n<tex2html_verbatim_mark>(1n
1000)<tex2html_verbatim_mark> and d<tex2html_verbatim_mark> , where n<tex2html_verbatim_mark> is the number of islands in the sea and d<tex2html_verbatim_mark> is the distance of coverage of the radar installation. This is followed by n<tex2html_verbatim_mark> 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轴上的一个区间,在这个区间内的任何一个点放置雷达,则可以覆盖该小岛,区间范围的计算用[x-sqrt(d*d-y*y),x+sqrt(d*d-y*y)];这样,问题即转化为已知一定数量的区间,求最小数量的点
#include<iostream> #include<cmath> #include<algorithm> using namespace std; int n,d; class M{ public : double x,y; bool operator<(M c)const{ return y<c.y; } void fun(){ double t=d*d-y*y; y=sqrt(t)+x; x=x-sqrt(t); } }; M m[1005]; bool judge(double x,M a){ return x>=a.x&&x<=a.y; } int main(){ int k=0; while(cin>>n>>d&&n&&d){ int x,y;int ok=1; int n1=n; for(int i=0;i<n;i++){ cin>>x>>y; if(y>d){ok=0;n1--;continue;} else { m[i].x=x; m[i].y=y; m[i].fun(); } } n=n1; if(ok==0){cout<<"Case "<<++k<<": -1"<<endl;;continue;} sort(m,m+n); int sum=0; for(int i=0;i<n;i++){ double t=m[i].y; while(i+1<n&&judge(t,m[i+1])){ i++; } sum++; } cout<<"Case "<<++k<<": "<<sum<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/demodemo/p/4716106.html