nyoj 题目287 Radar 贪心算法
来源:互联网 发布:ubuntu查看显卡使用率 编辑:程序博客网 时间:2024/06/05 04:26
Radar
时间限制:1000 ms | 内存限制:65535 KB
难度:3
- 描述
- 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.
- 输入
- 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 - 输出
- 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.
- 样例输入
3 21 2-3 12 11 20 20 0
- 样例输出
Case 1: 2Case 2: 1
- 来源
- Beijing 2002
- 上传者
- ctest大意:海上有n个海岛,图中y上半轴为海,下半轴为陆地。现布置一些雷达在x轴上,作用半径为d,求覆盖全部海岛的最少雷达数。输入第一行为n,d;以后的n行为n个海岛的坐标x,y;以0,0代表结束,如果不能完全覆盖,输出-1.思路:如果雷达可以覆盖到海岛,那么以海岛为圆心,以d为半径画圆在x轴必相交(最坏也是相切),那么就有一段区间,在这个区间内雷达在哪都能覆盖这个海岛,如果两个海岛作圆,如果区间有重叠,在重叠处放置一个雷达就可覆盖两个海岛,以此类推。那么就转化成了贪心算法中区间找点的问题,将每个海岛在x轴形成的坐标区间,保存到数组,并排序,再判断。
#include<stdio.h>#include<stdlib.h>#include<math.h>struct fun{double top;//左坐标 double last;//右坐标 }s[1000];int cmp(const void*a,const void*b){return (*(fun*)a).last>(*(fun*)b).last?1:-1;}int main(){int k=0;double n,r;while(~scanf("%lf %lf",&n,&r)){int flag=1;if(n==0&&r==0) break;double x,y;for(int i=0;i<n;i++){scanf("%lf %lf",&x,&y);if(y>r)//海岛距离太远,雷达半径不够,不能覆盖 {flag=0;break;}s[i].top = x-sqrt(r*r-y*y); s[i].last = x+sqrt(r*r-y*y);}qsort(s,n,sizeof(s[0]),cmp);//以右坐标从小到大排序 int sum=1;double temp=s[0].last ;//左边第一个肯定放置一个, for(int i=1;i<n;i++)//从第二个开始判断是否有重叠{if(s[i].top > temp)//左坐标大于上一个右坐标 ,不重叠,要放置雷达 {sum++;temp=s[i].last ;}}if(flag==1)printf("Case %d: %d\n",++k,sum);else printf("-1\n");}return 0;}
阅读全文
0 0
- nyoj 题目287 Radar 贪心算法
- NYOJ-287 Radar(贪心)
- poj 1328 Radar Installation(nyoj 287 Radar):贪心
- NYOJ 287 Radar 贪心之 区间选点
- poj 1328 && nyoj 287 Radar Installation【贪心】
- NYOJ 贪心算法题目汇总(一)
- nyoj 题目106 :背包问题 贪心算法
- nyoj 题目220 推桌子 贪心算法
- nyoj 题目 BUYING FEED 贪心算法
- nyoj 题目1036 非洲小孩 贪心算法
- NYOJ 287 Radar
- radar nyoj 287
- nyoj 287 Radar
- NYOJ-287 Radar
- NYOJ 287 Radar
- NYOJ Radar 贪心之区间选点问题
- 贪心算法之Radar Installation
- 南阳题目287-Radar
- 迭代器的使用iterator
- 自定义不同颜色点赞图片被点击之后动画缩放效果
- Attempt to invoke virtual method void android.support.v7.widget.SearchView.setSearchableInfo(androi
- ArcGIS Editor for OSM, 10.2.x Desktop安装问题
- CentOS源码包安装apache、nginx服务
- nyoj 题目287 Radar 贪心算法
- View 基础知识 ----------------------------(1)
- 实体池实现
- 系统大小端的头文件定义
- android 连接wifi
- equals() & hashCode()
- Uva 6609
- 二、Activiti5 核心API
- redis集群环境安装(参照redis中文官网,中间遇到了一些问题,so,记录一下)