Radar Installation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 60281 | Accepted: 13585 |
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
此题应转化对象并不是以海岸为分析点,而是以各个岛屿为中心画圆,看与海岸的交点范围,雷达若在这些范围内,此岛屿便可以被检测到。
点击打开链接#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
double x[1010],y[1010];
struct node
{
double l,r;
}q[1010];
double op(struct node x,struct node y)
{
return x.l<y.l;
}
int main()
{
int n,m,i,j,k,vis;
int s=0;
while(cin>>n>>k&&n&&k)
{
s++;int vis=0;
for(i=0;i<n;i++)
{
cin>>x[i]>>y[i];
if(y[i]>k)
vis=1;
}
if(vis==1)
{
printf("Case %d: -1\n",s);continue;//我直接输出了-1。。WA
}
printf("Case %d: ",s);
for(i=0;i<n;i++)
{
q[i].l=x[i]-sqrt(k*k*1.0-y[i]*y[i]*1.0);
q[i].r=x[i]+sqrt(k*k*1.0-y[i]*y[i]*1.0);
}
sort(q,q+n,op);
int ans=0;
double p=-0x3f3f3f3f;
for(i=0;i<n;i++)
{
if(p<q[i].l)//经常遵循小于小的大于大的原则
{
ans++;
p=q[i].r;
}
else if(p>q[i].r)//如果下一个区间的右端在上个区间的左p保留小值。
p=q[i].r;
}
cout<<ans<<endl;
}
return 0;
}
若干天重做心得:
1:大体的解题思路清晰,但是在对于处理区间仍有卡壳。
2:在处理计算重复最少区间时,应该初始化某个标准为无限,然后再遇到一个可加区间时(即当前标准小于当前区间的左端点,没有交集)累加并且,将此区间的右端点付给当前标准(这样才算是,跨过一个区间),然后继续比较,遇到除上一种情况(即,当前的区间右侧小于标准,那么就就尽量的让右端点小,以至于越靠近更多区间的交集部分)。