贪心 Radar Installation (求最少探测雷达)
2024-09-08 15:33:30
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 2
1 2
-3 1
2 1 1 2
0 2 0 0
Sample Output
Case 1: 2
Case 2: 1 先算出每个岛放探测雷达的x坐标范围,b为雷达探测半径,岛的坐标为(x,y),雷达的坐标范围是(x-sqrt(d*d-y*y),x+sqrt(d*d-y*y)),定义sum=1,i从1开始,每个坐标范围左右边界与上一个坐标范围的右边界比较,具体看代码。
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct stu
{
double l,r;
} st[];
bool cmp(stu a,stu b)
{
return a.l<b.l;
}
int main()
{
int a,s,k=,sum,i;
double b,x,y,sky;
while(scanf("%d %lf",&a,&b) &&!(a==&&b==))
{
s=;
k++;
for(i = ; i < a ; i++)
{
scanf("%lf %lf",&x,&y);
if(b < || y > b)
{
s=-;
}
st[i].l=x-sqrt(b*b-y*y);
st[i].r=x+sqrt(b*b-y*y);
}
if(s == -) printf("Case %d: -1\n",k);
else
{
sort(st,st+a,cmp);
sum=;
sky=st[].r;
for(i = ; i < a ; i++)
{
if(st[i].r <= sky)
{
sky=st[i].r;
}
else
{
if(st[i].l > sky)
{
sky=st[i].r;
sum++;
}
}
}
printf("Case %d: %d\n",k,sum);
} }
}
最新文章
- 深度剖析 | 基于大数据架构的BI应用
- RS-232 vs. TTL Serial Communication(转载)
- SQL/LINQ/Lamda 写法[转发]
- Java基础-重写System.out.println方法
- 普通View的measure流程
- [Html5]sessionStorage和localStorage的区别
- elasticsearch介绍集群,模拟横向扩展节点、节点宕机、改变分片
- DIV背景半透明文字不半透明的样式
- UVa 11572 Unique snowflakes【滑动窗口】
- 关于github在mac 10.10上无法提交代码的解决办法---备用
- 有理数类 Java BigInteger实现
- Visual Studio2013使用Microsoft Office Document Imaging(MODI)的方法
- Objective-C:KVC
- Linux进程和线程的比較
- Ubuntu Hash Sum mismatch 解决方法
- Java IO之File和IO
- tomcat启动不了,内存溢出
- &#9733;不容错过的PPT教程!
- linux使用.net core 创建简单的MVC
- 微信开发者工具_小程序js文件后面的M代表什么