POJ 1328 Radar Installation

预处理出每个岛所能容许的雷达范围,按照右端点排序,每次都新放置一个在新线段的右边,然后往后找哪个的左端点够不着了,再放在这个线段的右端点,以此类推

C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>

using namespace std;

struct land{
double l;
double r;
int y;
bool operator<(const land& b) const{
if(r == b.r){
return l < b.l;
}
return r < b.r;
}
}lands[1000];


int main()
{
int n, d;
int cas = 1;
bool flag = true;
int cnt = 1;
while(~scanf("%d%d", &n, &d) && n != 0){
flag = true;
cnt = 1;
int x;
for(int i = 0; i < n; i++){
scanf("%d%d", &x, &lands[i].y);
if(lands[i].y > d || lands[i].y < 0)
flag = false;
lands[i].l = x - sqrt(d*d - lands[i].y*lands[i].y);
lands[i].r = x + sqrt(d*d - lands[i].y*lands[i].y);
}
if(flag == false){
printf("Case %d: %d\n", cas++, -1);
continue;
}
sort(lands, lands+n);
double cur = lands[0].r;
for(int i = 1; i < n; i++){
if(lands[i].l > cur){
cur = lands[i].r;
cnt++;
}
}
printf("Case %d: %d\n", cas++, cnt);
}
return 0;
}