POJ-1328

Radar Installation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 89640   Accepted: 20135

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

 

题意:

有n座岛,在x轴上最少建立多少半径为d的雷达可以覆盖所有的岛。

 

贪心。

首先以岛为圆心,d为半径求出x轴上符合的区间,再将区间按左到右的顺序排序,若当前点的左端点大于之前最大的右端点,则++。更新最右端点。

 

AC代码:

 1 #include<iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <math.h>
 6 #include <algorithm>
 7 //#include<bits/stdc++.h>
 8 using namespace std;
 9 
10 struct node{
11     double le,ri;
12 }a[2010];
13 
14 int cmp(node a,node b){
15     return a.le<b.le;
16 }
17 
18 int x[2010],y[2010];
19 
20 int main(){
21     ios::sync_with_stdio(false);
22     int ans=0;
23     int n,d,num,flag;
24     double z,temp;
25     while(cin>>n>>d){
26         if(n==0&&d==0) break;
27         ans++;
28         flag=0;
29         for(int i=0;i<n;i++){
30             cin>>x[i]>>y[i];
31             if(y[i]>d)
32             flag=1;
33         }
34         if(flag){
35             cout<<"Case "<<ans<<": -1"<<endl;
36         }
37         else{
38             for(int i=0;i<n;i++){
39                 z=sqrt(d*d*1.0-y[i]*y[i]*1.0);
40                 a[i].le=(double)x[i]-z;
41                 a[i].ri=(double)x[i]+z;
42             }
43             sort(a,a+n,cmp);
44             temp=-100000000;
45             num=0;
46             for(int i=0;i<n;i++){
47                 if(a[i].le>temp){
48                     num++;
49                     temp=a[i].ri;
50                 }else if(a[i].ri<temp){
51                     temp=a[i].ri;
52                 }
53             }
54             cout<<"Case "<<ans<<": "<<num<<endl;
55         }
56     }
57     return 0;
58 } 

 

posted @ 2017-08-08 11:17  Kiven#5197  阅读(185)  评论(0编辑  收藏  举报