Radar installation
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:27114 |
|
Accepted:5912 |
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 abve 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 12 11 20 20 0
Sample output
Case 1: 2 Case 2: 1
Source
Beijing 2002 greedy:
# Include < Stdio. h >
# Include < Iostream >
# Include < Math. h >
# Include < Algorithm > // The library file where the sort is located.
Using Namespace STD;
Const Int Maxn = 1005 ;
Struct Line
{
Double L, R;
} Line [maxn]; // Each island is a circle with a radius of D and a line segment intercepted by the X axis.
Bool CMP (line A, line B)
{
Return A. L < B. L;
} // Sort by the Left endpoint of a line segment from small to large
Int Main ()
{
// Freopen ("test. In", "r", stdin );
// Freopen ("test. Out", "W", stdout );
Int N, D;
Int I;
Int X, Y;
Bool Yes; // Are you sure you have a solution?
Int Icase = 1 ;
While (CIN > N > D)
{
Yes = True ;
Int CNT = 0 ;
If (N = 0 && D = 0 ) Break ;
For (I = 0 ; I < N; I ++ )
{
CIN > X > Y;
If (Yes = False ) Continue ;
If (Y > D) Yes = False ;
Else
{
Line [I]. L = ( Double ) X - SQRT (( Double ) D * D - Y * Y );
Line [I]. r = ( Double ) X + SQRT (( Double ) D * D - Y * Y );
}
}
If (Yes = False )
{
Cout < " Case " < Icase ++ < " :-1 " < Endl;
Continue ;
}
Sort (line, line + N, CMP );
CNT ++ ;
Double Now = Line [ 0 ]. R;
For (I = 1 ; I < N; I ++ )
{
If (Line [I]. r < Now) // This is important.
Now = Line [I]. R;
Else If (Now < Line [I]. l)
{
Now = Line [I]. R;
CNT ++ ;
}
}
Cout < " Case " < Icase ++ < " : " < CNT < Endl;
}
Return 0 ;
}