poj1328Radar Installation(贪心)

来源:互联网 发布:服装批发开单软件 编辑:程序博客网 时间:2024/06/11 21:34
Radar Installation
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 54763 Accepted: 12342

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 21 2-3 12 11 20 20 0

Sample Output

Case 1: 2Case 2: 1
#include<stdio.h>#include<math.h>#include<iostream>#include<algorithm>using namespace std;struct st{double xl;double xr;}island[1001];int cmp(st a,st b){return a.xl<b.xl;}int search(st island[],int n){int i,num;num=1;double cur=island[0].xr;for(i=1;i<n;i++){if(island[i].xl>cur){num++;cur=island[i].xr;}else if(island[i].xr<cur){cur=island[i].xr;}}return num;}int main(){int n,d,i,j,count=0;int x,y;while(scanf("%d%d",&n,&d)&&(n+d)){count++;int flag=0;for(i=0;i<n;i++){double pos;scanf("%d%d",&x,&y);if(y>d){flag=1;} pos=sqrt(d*d-y*y);island[i].xl=x-pos;island[i].xr=x+pos;}if(flag){printf("Case %d: -1\n",count);continue;}else{sort(island,island+n,cmp);j=search(island,n);printf("Case %d: %d\n",count,j);}}return 0;}/*以下是解题思路:这是一道贪心问题,虽然一开始没有想到是贪心。。 相对于远点而言,我们假设,x轴的做半轴是左边,x轴的右半边是右边,  我们从左到右依次寻找,我们找一个岛屿能被雷达检测到的极限范围,在雷达监测范围(圆)的左半圆或圆的右半圆上, 即岛屿到雷达的范围是d 时,雷达可以再圆的左边,也可以在圆的右边,这样就可以群定雷达在岛屿坐标x的最左坐标和x的最右坐标。 最左坐标:xleft=x-sqrt(d*d-y*y),  最右坐标:xright=x+sqrt(d*d-y*y), 每个岛屿都有这样的最左和最右可被检测坐标 。 每次都将最右坐标作为衡量标准。 假设当前岛屿为cur,下一个岛屿为next, 1,如果next 的最左可被监测坐标大于cur 的最右可被监测坐标,那么雷达数加1, 2,如果next 的最左可被监测坐标小于cur 的最右可悲监测坐标,可分两种情况 A,next最右>=cur最右 B,next最右<cur最右 对于A中情况,我们可以直接检测到next了,可以找next的next了,对于B种情况 ,就相当于next包含cur,这样就可以以next的最右作为衡量标准了,  因为这样可以左移最右坐标, 可以让可能更多的岛屿被侦测到(他们的最左与衡量标准有更多的交集)*/ 


0 0