http://poj.org/problem?id=1328
Radar Installation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 90991 | Accepted: 20407 |
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
Source
没记错得话也是某届HNACM省赛的原题- -当时以为是计算几何什么的
一个很容易猜到的贪心是从左至右遍历,遇见没覆盖的点时尽可能的在保证覆盖此点的情况下往右边建。但是这是一个错误的贪心方案,圆心向右移动的同时最高点也将向右移动,这有可能导致原本可以覆盖住的点漏了出来导致答案错误,很忧桑。正解是对于每一个点考虑圆心可能的位置,显然每个点对应一条在x轴上的线段,这就转化为了一个经典的贪心问题,用最小点覆盖所有区间,按r排序后贪心建在最右边即可。
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 using namespace std; 7 #define eps 1e-8 8 struct node{double x,y;}P[1005]; 9 bool cmp(node A,node B) 10 { 11 if(A.y==B.y) return A.x<B.x; 12 return A.y<B.y; 13 } 14 int main() 15 { 16 int N,D,i,j,k=0; 17 while(cin>>N>>D&&(N||D)){int ans=0,ok=0; 18 double x,y; 19 for(i=0;i<N;++i) 20 { 21 scanf("%lf%lf",&x,&y); 22 double r=sqrt(D*D-y*y); 23 P[i].x=x-r; 24 P[i].y=x+r; 25 if(abs(y)>D)ok=1; 26 } 27 sort(P,P+N,cmp); 28 double en=-999999999; 29 for(i=0;i<N;++i) 30 { 31 if(P[i].x>en){ 32 ans++; 33 en=P[i].y; 34 } 35 } 36 if(ok) ans=-1; 37 printf("Case %d: %d ",++k,ans); 38 } 39 return 0; 40 }