poj 1328 Radar Installation (逆思维+贪心)

来源:互联网 发布:中国纯爱电影知乎 编辑:程序博客网 时间:2024/06/03 20:07

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 21 2-3 12 11 20 20 0
Sample Output
Case 1: 2Case 2: 1



大致题意:将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量。

思路:

以每个小岛做圆心,半径为d,交在线上,用一个区间表示,这一段的雷达都有影响到这个小岛,逆向思维。然后就是贪心找这一堆线段,最少几个点可以覆盖到所有线段,按照右端点排序从小到大,枚举每个区间,这个区间后面的区间的所有右端点一定比这个区间的右端点大,然后枚举所有他后面的区间,如果后面的左端点小于这个区间的右端点,说明这个区间有交集,用一个雷达就行,把之前合在一起的都标记掉。

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>using namespace std;const int maxn = 1e3 + 5;int n, d, book[maxn];struct node{    int l, r;}a[maxn];int cmp(node a, node b){return a.r < b.r;}int main(){int n, k, ca = 1;while(~scanf("%d%d", &n, &d), n+k){memset(book, 0, sizeof(book));int x, y, flag = 0;for(int i = 1; i <= n; i++){scanf("%d%d", &x, &y);if(y > d) flag = 1;else{int len = sqrt(d*d - y*y);a[i].l = x - len;a[i].r = x + len;}}if(flag) {cout << -1 << endl;continue;}sort(a+1, a+1+n, cmp);int ans = 0;for(int i = 1; i <= n; i++){if(book[i]) continue;book[i] = 1;for(int j = i+1; j <= n; j++){if(a[j].l <= a[i].r)book[j] = 1;}ans++;}printf("Case %d: %d\n", ca++, ans);}return 0;}


原创粉丝点击