http://poj.org/problem?id=1328
The question test instructions is that there are more than N islands on the sea, and a radar on the coastline (x-axis) can cover the island with its distance not exceeding D, and the minimum number of radars to cover all the islands.
.
.
.
.
.
.
.
.
.
The greedy idea is to convert a point to an interval on the x-axis coordinates (that is, a set of all possible locations that can guarantee coverage of the island), and then row in the order of points, or at the left end of the row. Then the leftmost side of the right traversal, if the left end of the next interval on the right side of the previous radar, obviously need to put a new radar; If you want to leave, then you need to judge the right end, if the right end is also on the last radar left, then this radar obviously can not cover the current small island, The radar position needs to be adjusted to the right end of the current interval, so that both the previous and the present can be covered, and if the right end is at the right end of the previous radar, no adjustment is required and no new radars need to be placed.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <
Algorithm> using namespace std;
struct Node {double Zuo, you;} fei[2100], t;
int CMP (Node A, Node B) {return A.zuo < B.zuo;} int x[2100],y[2100];
int main () {int I, j, N, D, NUM, flag, s=0;
Double Z, p;
while (scanf ("%d%d", &n,&d)!=eof) {if (n==0&&d==0) break;
s++;
flag=0;
for (i=0; i<n; i++) {scanf ("%d%d", &x[i],&y[i]);
if (y[i]>d) flag=1;
} if (flag) printf ("Case%d: -1\n", s);
else {for (i=0; i<n; i++) {z=sqrt (d*d*1.0-y[i]*y[i]*1.0);
fei[i].zuo= (double) x[i]-z;
fei[i].you= (double) x[i]+z;
} sort (fei,fei+n,cmp);
num=0;
p=-100000000;
for (i=0; i<n; i++) {if (fei[i].zuo>p) {num++;
P=fei[i].you;
} else if (fei[i].you<p) {p=fei[i].you;
}} printf ("Case%d:%d\n", s,num);
}} return 0;
}