Radar installation
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:52823 |
|
Accepted:11883 |
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
Idea: there must be a region on the X axis. Radar can overwrite a small island in this region. Then, sort these regions by the Left interval, and then solve the problem with greed.
All these tests can pass through, because a variable has been wrong for a long time!
# Include "stdio. H "# include" string. H "# include" math. H "# include" iostream "# include" algorithm "using namespace STD; # define n 1005 struct node {double L, R;} p [N]; Double X [N], Y [N]; bool CMP (node A, Node B) {return. L <B. l;} int main () {int I, n, D, CNT = 1, flag; while (scanf ("% d", & N, & D ), N | D) {flag = 0; for (I = 0; I <n; I ++) {scanf ("% lf", & X [I], & Y [I]); If (FLAG) continue; If (d <Y [I]) Flag = 1; double T = SQRT (D * D-y [I] * Y [I]); P [I]. L = x [I]-T; P [I]. R = x [I] + T;} printf ("case % d:", CNT ++); If (FLAG) {printf ("-1 \ n "); continue;} Sort (p, p + N, CMP); int T = 1; double RR; RR = P [0]. r; for (I = 1; I <n; I ++) {If (P [I]. l> RR) // The range cannot be overwritten by radar {RR = P [I]. r; t ++;} If (P [I]. r <RR) // can be used by the current radar, {RR = P [I]. r ;}} printf ("% d \ n", T);} return 0 ;}
Poj 1328 radar installation (Greedy)