poj1328雷达设置 贪心

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
这道题目与下面的题目有异曲同工之妙

【贪心】雷达问题

 

题目描述

张琪曼等人用时空雷达定位李旭琳所在的空间位置。如图7.3所示,时空雷达装在一条直线上,直线上方是空间海洋,每个人在空间海洋的位置就如同大海中的岛屿,这些人的位置已知,每一个雷达的扫描范围是一个半径为d的圆形区域,问最少需要多少个雷达覆盖所有的人(岛屿)。

输入

输入包括多组测试数据,每组测试数据第一行为两个整数n (1≤n≤1000) 和 d,即岛屿数和雷达扫描半径。随后n行每行两个整数表示岛屿坐标。每组测试数据以空行间隔,所有测试数据以0 0结束。

输出

输出最少需要安装雷达数,每组一行。若无解以-1表示。
 
本想用以点为原点,R为半径x轴的与左端点排序,扫一遍就好了,然而突然想到一种情况,会使左端点满足情况,但右端点不满足。所以要更新右端点。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>


using namespace std;
const int maxn = 1005;
struct uct {
    double left, right;
};
bool cmp(uct a, uct b) {
    return a.left < b.left;
}

int main() {
    ios::sync_with_stdio(false);
    uct s[maxn];
    int  k = 0, n, R;

    while(cin >> n >> R) {
        int  flag = 1;
        if(n == 0&& R == 0) break;
        for(int i = 0; i < n; i++){
            int x, y;
            cin >> x >> y;
            if(abs(y) <= R){
                double len = sqrt((R*R - y*y)*1.0);
                s[i].left = x - len;
                s[i].right = x + len;
            }else flag = 0;
            //cout << i <<" " <<x <<" " <<y << endl;
        }
        if(flag) {
            sort(s, s+n, cmp);
            int cnt = 1;
            double ends = s[0].right;
            for(int i = 0; i < n; i++){
                if(s[i].left > ends){
                    ends = s[i].right;
                    cnt++;
                }else if(s[i].right  < ends){
                    ends = s[i].right;
                }
            }
            cout << "Case " << ++k << ": " << cnt << endl;
        }  else
         cout << "Case " << ++k << ": " << -1 << endl;
    }

    return 0;
}
最近又懒了

 

 

posted on 2016-07-22 00:21  disppr  阅读(467)  评论(0编辑  收藏  举报