poj 1328--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
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
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
这题的大意就是给你land的坐标,现在要建立雷达站,问怎样建立使得用最少的雷达包含所有的land
这题是第一个专题想的最久的一题= =训练计划上写的是贪心思想。可就是没想到怎么个操作。。
后来在纸上画画,才发现了突破点。以每个站点为圆心,d为半径画圆,会与x轴相交,若相离,正好说明了不存在的情况。通过相交的点的坐标来确定该点周围 建立雷达的最左和最右范围。每两个半圆会出现相离,相交和内嵌的情况,前两者可以以最小的那个lft的rgt为出发点,只要包含于它左边的不用记,因为肯 定能涉及到,如果一个lft大于了这个rgt,说明有新的出现,更新即可=。=对于第三种情况,如果又一半圆与内嵌的圆相交,如果按照前面方法做的话,会 少建立一个。所以此时必须改变它的rgt,使他是内嵌的那个小圆的最右值。。。
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 struct island 7 { 8 double lft; 9 double rgt; 10 }p[1100]; 11 bool cmp(island a,island b) 12 { 13 return a.lft==b.lft?a.rgt<b.rgt:a.lft<b.lft; 14 } 15 int main() 16 { 17 int n,i,cas=1; 18 double d,x,y,z; 19 while(scanf("%d%lf",&n,&d)&&(d+n)) 20 { 21 int flag=1; 22 for(i=0;i<n;i++) 23 { 24 scanf("%lf%lf",&x,&y); 25 if(y>d) 26 { 27 flag=0; 28 continue; 29 } 30 z=d*d-y*y; 31 p[i].lft=x-sqrt(z); 32 p[i].rgt=x+sqrt(z); 33 } 34 printf("Case %d: ",cas++); 35 if(!flag) 36 { 37 printf("-1\n"); 38 continue; 39 } 40 sort(p,p+n,cmp); 41 int cnt=0; 42 double maxx=p[0].rgt; 43 for(i=0;i<n;i++) 44 { 45 if(p[i].lft>maxx) 46 { 47 cnt++; 48 maxx=p[i].rgt; 49 } 50 if(p[i].rgt<maxx) 51 maxx=p[i].rgt; 52 } 53 printf("%d\n",cnt+1); 54 } 55 return 0; 56 }