Radar Installation
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 60 Accepted Submission(s) : 11
Problem 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.
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
PKU
今天通过这道题又学习了一下贪心算法的知识,感觉收获很多。
对于每个岛屿的位置,如果y大于d则雷达无法侦测到岛屿,结果为-1;如果y都小于d,将每个岛屿按照x坐标从小到大排列,并算出对于每个岛屿,一个雷达能侦测到这个岛屿时雷达在x轴上的最左和最右的位置,并储存为lx和rx。从最左边的岛屿算起,如果该岛屿(记为a1)右边下一个岛屿(记a2)的lx大于a1的rx,则只能再加一个雷达才能侦测到a2;如果a2的lx小于a1的rx,且a2的rx小于a1的rx则a2的再下一个岛屿(记为a3)的lx必须小于a2的rx才能被原来的雷达检测到,否则要再加一个雷达(即a3必须和a2的rx比较);如果a2的lx小于a1的rx,且a2的rx大于a1的rx,能侦测到两个岛屿的雷达坐标范围是a2的lx到a1的rx,则a3必须和a1的rx进行比较来判断是否应该再加一个雷达。
#include<iostream> #include<cmath> #include<algorithm> using namespace std; struct island { int x,y; double rx,lx; }; int cmp(const island &a,const island &b) { if(a.x<b.x) return 1; else return 0; } int main() { int cas=0,n,d; while(cin>>n>>d&&n+d) { cas++; island is[1001]; bool f=true; for(int i=0;i<n;i++) { cin>>is[i].x>>is[i].y; if(is[i].y>d) f=false; double t=sqrt(d*d-is[i].y*is[i].y); is[i].lx=is[i].x-t; is[i].rx=is[i].x+t; } if(!f) { cout<<"Case "<<cas<<": "<<-1<<endl; } else { sort(is,is+n,cmp); /*for(int i=0;i<n;i++) cout<<is[i].x<<' '<<is[i].rx<<' '<<is[i].lx<<endl;*/ double temp=is[0].rx; int count=1; for(int i=1;i<n;i++) { if(is[i].lx>temp) { count++; temp=is[i].rx; } else if(is[i].rx<temp) temp=is[i].rx; } cout<<"Case "<<cas<<": "<<count<<endl; } } }