Problem-solving ideas: given n islands, N Islands coordinates are (A1,B1), (A2,B2)-----(AN,BN), radar coverage radius r
The minimum number of radars required for all islands to be covered.
The island coordinates are processed first, because the radar is positioned on the x-axis, so we have the coordinates of the radar (x,0), for any one of the islands P (A, A, b), because the island to meet the radar coverage, so (X-A) ^2+b^2=r^2, the solution
XMIN=A-SQRT (r*r-b*b);//is the left end of the interval xmax=a+sqrt (R*R-B*B);//is the right end of the interval
The next range of points can be
------------------------------tmp
1 A[i]----------------------------B[i]
2 a[i]--------B[i]
3 A[i]--------------------B[i]
Using TMP to record the current radar coordinates, the interval is sorted by the left endpoint in ascending order, from left to right scan, there will be 3 kinds of cases
1 Current Tmp<a[i], radar can not cover to the next interval, so add a new radar, and update the coordinates of the radar is the right end of the interval (greedy, on the right, the more likely to cover the next interval), that is tmp=b[i]
2 current B[I]<TMP, the radar can not cover the range, but the interval is included in the TMP range, so do not need to increase the radar, update the value of TMP Tmp=b[i]
3 The range is covered by radar and is not processed.
Radar Installation
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 55518 |
|
Accepted: 12502 |
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
#include <stdio.h> #include <string.h> #include <math.h>double a[1005],b[1005];void bubblesort ( Double a[],double b[],int N) {int i,j;double t;for (i=1;i<=n;i++) {for (j=i+1;j<=n;j++) {if (A[i]>a[j]) {T=a[i];a [I]=a[j];a[j]=t;t=b[i];b[i]=b[j];b[j]=t;}}} int main () {int n,i,sum,flag,tag=0;double x,y,r,tmp;flag=1;while (scanf ("%d%lf", &n,&r)!=eof&& (n| | R) {tag=0;for (i=1;i<=n;i++) {scanf ("%lf%lf", &x,&y), if (R>=fabs (y)) {a[i]=x-sqrt (r*r-y*y); b[i]=x+ sqrt (r*r-y*y); } else tag=1; } The IF (tag)//Does not consider the situation of r<0 can also be passed printf ("Case%d: -1\n", flag++); else{ Bubblesort (a,b,n); Sum=1; TMP=B[1]; for (i=2;i<=n;i++) {if (a[i]>tmp) {tmp=b[i];sum++;} else if (b[i]<tmp) tmp=b[i]; } printf ("Case%d:%d\n", flag++,sum);}}}
POJ 1328 Radar Installation "greedy zone"