Description
Assume the coasting is a infinite straight line. Side of coasting, sea in the other. Each of small island was a point locating in the sea side. and any radar installation, locating on the coasting, can only cover D-distance, so a island in the sea can is 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 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 a 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 all case contains-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 was followed by n lines each containing and 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 all test case output one line consisting of the test case number followed by the minimal number of radar installation S needed. "-1" installation means no solution for this case.
Sample Input
3 21 2-3 12 11 20 20 0
Sample Output
Case 1:2case 2:1
This problem is greedy thinking, for each island, can be covered on the x-axis can be overlaid on the island of the interval calculated, that is [X-sqrt (D*d-y*y), X+sqrt (d*d-y*y)], you can first to all the interval segments by the right endpoint ascending order, and then find the smallest right end point, After finding all the left endpoints less than this right end of the record a marker B[i].vis=1, which means that the line is already a bit, and will not have to look back. This problem can also use another way of thinking, all the interval segments in descending order of the left endpoint, the initial right endpoint is recorded as S=B[1].R, if the next segment of the left end of the value is greater than S, then to add a mine point, and S=B[I].R, if the right endpoint of the segment is less than S, then update s=b[i]. R. The nature of the two ideas is actually the same.
Idea One:
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm>using namespace Std;struct node{Double l,r;int vis;} B[1005];bool CMP (node A,node b) {Double temp;if (A.R>B.R) {Temp=b.l;b.l=a.l;a.l=temp;Return a.r<b.r;}return A.R<B.R;} int main () {Double d,x,y;int I,j,n,num,m=0,flag;while (scanf ("%d%lf", &n,&d)!=eof){m++;if (n==0 && d==0) break;flag=1;Memset (b,0,sizeof (b));for (i=1;i<=n;i++) {scanf ("%lf%lf", &x,&y);if (y>d) {flag=0; Don't use break here, because the data is going to be lost.}B[I].L=X-SQRT (D*d-y*y);B[I].R=X+SQRT (D*d-y*y);}if (flag==0) {printf ("Case%d: -1\n", m); continue;}Sort (b+1,b+1+n,cmp);num=0;for (i=1;i<=n;i++) {if (b[i].vis==0) {Num++;b[i].vis=1;for (j=1;j<=n;j++) {if (B[J].L<=B[I].R) {B[j].vis=1;}}}}printf ("Case%d:%d\n", m,num);}return 0;}
Idea two:
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm>using namespace Std;struct node{Double L,r;} B[1005];bool CMP (node A,node b) {Double temp;if (A.L>B.L) {Temp=b.r;b.r=a.r;a.r=temp;Return a.l<b.l;}return A.L<B.L;} int main () {Double d,x,y,s;int I,j,n,num,m=0,flag;while (scanf ("%d%lf", &n,&d)!=eof){m++;if (n==0 && d==0) break;flag=1;Memset (b,0,sizeof (b));for (i=1;i<=n;i++) {scanf ("%lf%lf", &x,&y);if (y>d) {flag=0;}B[I].L=X-SQRT (Double) (d*d-y*y));B[I].R=X+SQRT (Double) (d*d-y*y));}if (flag==0) {printf ("Case%d: -1\n", m); continue;}Sort (b+1,b+1+n,cmp);Num=1;S=B[1].R;for (i=2;i<=n;i++) {if (b[i].l>s) {num++;S=b[i].r;continue;}if (b[i].r<s) {S=B[I].R;Continue}}printf ("Case%d:%d\n", m,num);}return 0;}
poj1328 Radar Installation