Radar Installation
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 54798 |
|
Accepted: 12352 |
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
Test instructions: is to find the least station, to cover all the points.
Idea: We can do a circle with a radius of D, and intersect with the x-axis, assuming that it does not intersect then the task will not be completed, and the other is transformed into an interval point.
Code:
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h>using namespace STD; #define M 1005struct Node {double St, en;} S[m];int CMP (Node A, Node B) {if (A.en = = b.en) return a.st > B.st;return a.en<b.en;} int main () {int n, v = 1; double D;while (scanf ("%d%lf", &n, &d), n| | d) {int I, j;double a, B;int flag = 0;for (i = 0; i < n; i + +) {scanf ("%lf%lf", &a, &b); if (b>d) flag = 1;IF (fla G = = 0) {S[i].en = A+sqrt (d*d-b*b); s[i].st = A-sqrt (d*d-b*b);//printf ("%lf%lf%d.. \ n ", S[i].st, S[i].en, i);} scanf ("%lf%lf", &s[i].st, &s[i].en);} printf ("Case%d:", v++); if (flag) {printf (" -1\n"); Continue;} Sort (S, s+n, CMP); int ans = 1;double MaxR = S[0].en;i = 1, j = 0;while (I < n) {if (s[i].st <= s[j].en) {//if (MaxR < S[i].en) MaxR = s[i].en;++i;} else {//if (j = = i-1) J = I;++ans;//maxr = S[i].en;}} printf ("%d\n", ans);} return 0;}
POJ 1328 Radar Installation "greedy" "interval problem"