1. Link:
Http://poj.org/problem? Id = 1328
Http://bailian.openjudge.cn/practice/1328/
2. content:
Radar installation
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:52833 |
|
Accepted:11891 |
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: 2Case 2: 1
Source
Beijing 2002
3. Method:
(1) Find the area where the Lighthouse can be installed on each island and use the struct to represent lines.
(2) According to the rightmost fast sorting, pay special attention to how to write Compare of the double type.
(3) greedy algorithm for minimum beacon count
4. Code:
# Include <iostream>
# Include <cstdio>
# Include <cmath>
# Include <cstdlib>
Using namespace STD;
Struct line
{
Double L;
Double R;
};
Int CMP (const void * a, const void * B)
{
Line line1 = * (line *) );
Line line2 = * (line *) B );
/* If (line1.r = line2.r)
{
If (line1.l = line2.l) return 0;
Return line1.l> line2.l? 1:-1;
}
Else return line1.r> line2.r? 1:-1 ;*/
If (line1.r = line2.r) return 0;
Else return line1.r> line2.r? 1:-1;
}
Int main ()
{
// Freopen ("D: // input.txt", "r", stdin );
Int I;
Int N, D;
Int COUNT = 1;
Int X, Y;
Int flag;
Double INS;
Cin> N> D;
While (n! = 0 | D! = 0)
{
Line * lines = new line [N];
Flag = 1;
For (I = 0; I <n; ++ I)
{
Cin> x> Y;
If (d <Y) Flag = 0;
Else
{
INS = SQRT (D * D-y * y );
Lines [I]. L = x-ins;
Lines [I]. r = x + INS;
}
}
If (flag = 0) cout <"case" <(count ++) <":-1" <Endl;
Else
{
Qsort (lines, N, sizeof (line), CMP );
/* For (I = 0; I <n; ++ I)
{
Cout <lines [I]. L <"" <lines [I]. r <Endl;
}*/
Int num = 1;
Double max_r = lines [0]. R;
For (I = 1; I <n; ++ I)
{
If (max_r <lines [I]. l)
{
Num ++;
Max_r = lines [I]. R;
}
}
Cout <"case" <(count ++) <":" <num <Endl;
}
Delete [] lines;
Cin> N> D;
}
// Fclose (stdin );
Return 0;
}
5: Reference:
Http://blog.sina.com.cn/s/blog_48f85e1d0100nslz.html
Poj 1328/openjudge 1328 radar installation