Radar Installation(poj1328,贪心,区间交集问题)

来源:互联网 发布:社交网络的坏处 编辑:程序博客网 时间:2024/05/23 01:13

http://poj.org/problem?id=1328

Radar Installation

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 41863 Accepted: 9262

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

Beijing 2002

[Submit]   [Go Back]   [Status]   [Discuss]

 Home Page   Go Back  To top

解析:

题意:

在一个平面坐标内,给出n个岛屿的位置以及雷达所能覆盖的距离R,且雷达只能在X轴上且只能覆盖第一第二平面

(即y>=0部分)问最少需要多少个雷达,否则输出'-1';

思路:

转化为判断区间交集问题

1.利用勾股定理给每个岛屿设定雷达设置地点范围;

2.再将其排序从左到有判断区间是否有交集,如果有更替当前区间,否则增设一个地雷。

 注:判断区间交集,我这里使用的更新当前区间法,即如果与上个区间存在交集,就把当前区间更新为与前一个区间的公共部分,这样便很快且不遗漏的区分多个区间是否有交集

Memory Time Language Code Length Submit Time

200K 16MS C++ 1100B 2013-07-22 09:27:38

*/

#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<iostream>using namespace std;const int maxn=1000+10;int n,flag;double R;struct point{double r;double l;}p[maxn];bool cmp(point a1,point a2){return a1.l<a2.l||(a1.l==a2.l&&a1.r<a2.r);}void init(){ double x,y;for(int i=0;i<n;i++)  {  scanf("%lf%lf",&x,&y);  if(y<0||y>R)  flag=0;  double t=sqrt(R*R-y*y);//由勾股定理可得  p[i].l=x-t;  p[i].r=x+t;  }}double min(double a,double b){return a<b? a:b;}double max(double a,double b){return a>b? a:b;}void  solve(){  int ans=1;sort(p,p+n,cmp);double s=p[0].l;double t=p[0].r;for(int i=1;i<n;i++){if(p[i].l>t){ans++;s=p[i].l;t=p[i].r;}else//缩小区间,寻找公共区间{s=max(s,p[i].l);t=min(t,p[i].r);}}printf("%d\n",ans);}int main(){   int c=0;while(scanf("%d%lf",&n,&R)!=EOF&&!(n==0&&R==0)){flag=1;if(R<0)flag=0;init();printf("Case %d: ",++c);if(flag==0)printf("-1\n");elsesolve();}return 0;}


原创粉丝点击