题目:http://poj.org/problem?id=1328
贪心:按区间右端点排序 岛屿对应放置雷达的区间
在区间的右端点放置雷达,若之后区间的左端点
1.<=上一个雷达,说明被覆盖,跳过
2.>上一个雷达,则在这个区间右端点放置雷达
若某个区间左端点>上一个雷达,那么这个区间至少要有一个雷达,
故产生了安装雷达数目的下界,下界的可构造性由程序给出。
#include <stdio.h>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 1000 + 5;
struct pos {
double x = 0;
double y = 0;
bool operator()(const pos& a, const pos& b) {
return a.y < b.y;
}
};
bool cmp(const pos& a, const pos& b) {
return a.y < b.y;
}
int main() {
pos p[MAXN];
int counter[MAXN];
for (int i = 0; i < MAXN; ++i)
counter[i] = 1;
int _count = 0;
int n;
double d;
while (true) {
scanf_s("%d%lf", &n, &d);
if (n == 0)break;
double x, y;
bool sign = true;
for (int i = 0; i < n; ++i) {
scanf_s("%lf%lf", &x, &y);
double disSquare = d*d - y*y;
if (disSquare < 0 || d < 0) {
counter[_count] = -1;
++_count;
sign = false;
++i;
for (; i < n; ++i)
scanf_s("%lf%lf", &x, &y);
break;
}
double delta = sqrt(disSquare);
p[i].x = x - delta;
p[i].y = x + delta;
}
if (!sign) {
getchar();
continue;
}
//std::sort(p, p + n, [](pos a, pos b)->bool { return a.y < b.y; });
//std::sort(p, p + n, pos());
std::sort(p, p + n, cmp);
int cur = 1;
double last = p[0].y;
while (cur < n) {
if (p[cur].x > last) {
++counter[_count];
last = p[cur].y;
}
++cur;
}
++_count;
getchar();
}
for (int i = 0; i < _count; ++i)
printf("Case %d: %d\n", i + 1, counter[i]);
system("pause");
return 0;
}
附测试数据:
2 5
-3 4
-6 3
4 5
-5 3
-3 5
2 3
3 3
20 8
-20 7
-18 6
-5 8
-21 8
-15 7
-17 5
-1 5
-2 3
-9 6
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 7
9 6
10 5
0 0
2 3
0 2
2 3
2 3
0 2
1 3
3 3
1 2
-3 2
2 4
8 5
2 4
-4 4
-3 3
-3 1
-3 0
-1 0
0 5
6 0
3 0
1 2
-3 1
2 1
3 2
1 2
-3 1
2 1
1 2
0 2
2 3
0 2
2 3
4 -5
4 3
4 3
2 3
6 -9
3 -3
1 2
-3 2
2 1
6 2
1 2
1 2
1 2
-3 1
2 1
0 0
1 2
0 2
2 3
0 2
1 3
3 10
1 10
2 3
4 5
3 5
1 10
2 3
4 5
4 7
1 10
2 3
4 5
0 0
3 9
1 10
2 3
4 5
0 0
答案
Case 1: 1
Case 2: 2
Case 3: 4
Case 4: 1
Case 5: 1
Case 6: -1
Case 7: 3
Case 8: -1
Case 9: 2
Case 10: 1
Case 11: 1
Case 12: -1
Case 13: -1
Case 14: 2
Case 15: 1
Case 16: 1
Case 17: 1
Case 18: -1
Case 19: -1
Case 20: -1