码迷,mamicode.com
首页 > 其他好文 > 详细

[2016-02-04][POJ][1328][Radar Installation]

时间:2016-02-05 01:55:48      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

[2016-02-04][POJ][1328][Radar Installation]

Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u

 Status

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
  • 时间:2016-02-04 22:01:05 星期四
  • 题目编号:POJ 1328
  • 题目大意:
    • 在坐标系上给定若干个点,在x轴上设置雷达,使得每个点都被雷达扫描,
    • 求最少雷达数目
  • 分析:
    • 可以计算出每一个点,对应的雷达在x轴的放置范围,
    • 那么题目就转化为,给定若干个区间,求最少点的数目,使得每一个区间都有一个点
    • 即,相交区间的数目
  • 方法:
    • 排序 按右端点 小→大(相等时,按左端点大→小)对区间进行排序,
    • 记录上一个区间,
    • 选择没有和上一个区间相交的区间
    • 计数+1
    • 重复
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
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1000 + 10;
struct room{
    double s,e;
    bool operator < (const room a)const {
        return e < a.e || (e == a.e && s > a.s);
    
}r[maxn];
 
int main(){
    int n,d;
    int cnt = 0; 
    //freopen("out.txt","w",stdout);
    while(~scanf("%d%d",&n,&d) && (n || d)){
        int x,y;
        int res = 1;
        for(int i = 0; i < n ;i++ ){
            scanf("%d%d",&x,&y);
            r[i].s = x - sqrt(((double)d*d - y*y));
            r[i].e = x + sqrt(((double)d*d - y*y));
            if (y > d)
                res = -1;
        }
         
        if(~res)
        {
            sort(r,r+n);
            room pre = r[0];
            for(int i = 1; i < n;i++){
                if(r[i].s > pre.e){
                    res++;
                    pre = r[i];
                }
            }
        }
        printf("Case %d: %d\n", ++cnt, res);
    }
    return 0;
}










[2016-02-04][POJ][1328][Radar Installation]

标签:

原文地址:http://www.cnblogs.com/qhy285571052/p/5182437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!