Radar Installation POJ - 1328
2024-10-13 03:54:40
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
该题题意是为了求出能够覆盖所有岛屿的最小雷达数目
这题一开始根本没有贪心思路,想不到
qu[i].left=x-sqrt(r*r-y*y);
qu[i].right=x+sqrt(r*r-y*y);
转化为一个区间问题。
关于这个的合理性 你们画个图就好了 ,表示不会电脑画图
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cctype>
using namespace std;
struct node
{
double left,right;
}qu[];
int cmp(node a,node b)
{
return a.right<b.right;
}
int main() {
int n,k=;
double r;
while(scanf("%d%lf",&n,&r)!=EOF){
if (n== && r== ) break;
int flag=;
double x,y;
for (int i= ;i<n ;i++){
scanf("%lf%lf",&x,&y);
if (!flag) continue;
if (y>r) {
flag=;
continue;
}
qu[i].left=x-sqrt(r*r-y*y);
qu[i].right=x+sqrt(r*r-y*y);
}
sort(qu,qu+n,cmp);
printf("Case %d: ",k++);
if (!flag) {
printf("-1\n");
continue;
}
int sum=;
double temp=-;
for (int i= ; i<n ;i++){
if (qu[i].left>temp){
sum++;
temp=qu[i].right;
}
}
printf("%d\n",sum);
}
return ;
}
最新文章
- 作业三(代码规范、代码复审、PSP)
- datagrid---写后台数据交互
- 实现跨域的N种方法
- 又是一个二模02,不过day2
- php的socket通信(一)
- Server编解码 解决Response.Redirect方法传递汉字丢失或乱码
- MVC 构造
- 解决:The Operation couldn&#39;t be completed.(LaunchServicesError error 0.)
- Codeforces Round #219 (Div. 2) E. Watching Fireworks is Fun
- JS关闭窗口或JS关闭页面的几种代码
- [工作] 使在家办公(Work From Home)更有效率的建议
- toast组件较为完美版本
- Monkey 命令使用说明
- linux下登陆mysql失败
- valgrind简介以及在ARM上交叉编译运行【转】
- NodeJS Stream流
- 转载:分布式文件系统 - FastDFS 在 CentOS 下配置安装部署(1)
- LeetCode 806 Number of Lines To Write String 解题报告
- 原创工具binlog2sql:从MySQL binlog得到你要的SQL
- python dict sorted 排序
热门文章
- BZOJ 1485: [HNOI2009]有趣的数列 [Catalan数 质因子分解]
- (python功能定制)复杂的xml文件对比,产生HTML展示区别
- 二维码开源库ZBar-windows下编译和使用
- 孤立的SQL用户
- S5PV210时钟,看门狗定时器
- Swift iOS 日期操作:NSDate、NSDateFormatter
- Linux双网卡搭建NAT服务器之网络应用
- C++11 标准库也有坑(time-chrono)
- appium滑动操作(向上、向下、向左、向右)
- 知识点干货--讲一讲final、finally、finalize的区别