Radar Installation
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 Source |
解题思路:
开始的时候,没有想到是区间贪心,直接想的是以每个点作为圆的边界,然后求出圆心,将圆心内的点排除,然后重复此步骤。之后发现是错的,想了一下是转化成区间然后在贪心。
具体的过程就是,首先将每个点转化成区间,转化区间的方式为以每个点为圆心,半径为d(依题目中的描述),与x轴所交的两个点便是该区间。这时题意变转化为只在每个区间内选取一个点即可。之后就是熟悉的区间贪心了,依据x,y的降序排列区间,然后开始贪心区间。
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn = 1e3 + 6;
const double e = 1e-7;
struct node {
double x, y;
} a[maxn];
int n;
double d;
bool cmp(node a, node b)
{
if(a.x == b.x) {
return a.y < b.y;
} else {
return a.x < b.x;
}
}
void check() {
for(int i = 0; i < n; ++i) {
cout << a[i].x << " " << a[i].y << endl;
}
}
void change(double & a, double & b) {
double x, y;
x = a - sqrt(d * d - b * b);
y = a + sqrt(d * d - b * b);
a = x;
b = y;
}
int main()
{
int t = 1;
while(scanf("%d%lf", &n, &d) != EOF) {
if(n == 0 && d == 0) {
break;
}
bool flag = true;
if(d < 0) {
flag = false;
}
for(int i = 0; i < n; ++i) {
scanf("%lf%lf", &a[i].x, &a[i].y);
if(a[i].y > d + e || a[i].y < 0) {
flag = false;
}
change(a[i].x, a[i].y);
}
//check();
sort(a, a + n, cmp);
//check();
int f = 0;
int ans = 0;
node now;
while(f < n && flag) {
now = a[f];
ans++;
f++;
while(f < n) {
if(now.y < a[f].x - e) {
break;
} else {
now.x = max(now.x, a[f].x);
now.y = min(now.y, a[f].y);
}
f++;
}
}
if(flag) {
printf("Case %d: %d\n", t++, ans);
} else {
printf("Case %d: %d\n", t++, -1);
}
}
return 0;
}
这里有我从poj的discuss里面找的一些数据。
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