Radar Installation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 56826 | Accepted: 12814 |
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 above 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 2 1 2 -3 1 2 1 1 2 0 2 0 0
Sample Output
Case 1: 2 Case 2: 1
1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<math.h> 5 using namespace std; 6 struct island 7 { 8 double x , y ; 9 double left , right ; 10 }a[2000]; 11 int n ; 12 int d ; 13 bool cmp (island a , island b) 14 { 15 return a.x < b.x ; 16 } 17 18 int main () 19 { 20 // freopen ("a.txt" , "r" , stdin) ; 21 int cnt ; 22 int ans = 1 ; 23 bool flag ; 24 25 while (~ scanf ("%d%d" , &n , &d)) { 26 if (n == 0 && d == 0) 27 break ; 28 flag = 0 ; 29 30 for (int i = 0 ; i < n ; i++) { 31 scanf ("%lf%lf" , &a[i].x , &a[i].y) ; 32 if (a[i].y > 1.0 * d || a[i].y < 0 || d <= 0) { 33 flag = 1 ; 34 } 35 if (!flag) { 36 a[i].left = (double) a[i].x - sqrt (1.0 * d * d - a[i].y * a[i].y) ; 37 a[i].right = (double) a[i].x + sqrt (1.0 * d * d - a[i].y * a[i].y) ; 38 } 39 } 40 if (flag) { 41 printf ("Case %d: -1\n" , ans++) ; 42 continue ; 43 } 44 sort (a , a + n , cmp) ; 45 cnt = 1 ; 46 double l = a[0].left , r = a[0].right ; 47 for (int i = 1 ; i < n ; i++) { 48 if (a[i].left > r) { 49 cnt++ ; 50 l = a[i].left ; 51 r = a[i].right ; 52 } 53 else { 54 l = a[i].left ; 55 r = a[i].right < r ? a[i].right : r ; 56 } 57 } 58 printf ("Case %d: %d\n" , ans++ , cnt) ; 59 } 60 return 0 ; 61 }
别忘记每次都要跟新放radar的区间 ,orz