POJ 题目1328 Radar Installation(贪心)

来源:互联网 发布:淘宝卖家怎样设置运费 编辑:程序博客网 时间:2024/05/18 07:27
Radar Installation
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52282 Accepted: 11745

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 21 2-3 12 11 20 20 0

Sample Output

Case 1: 2Case 2: 1

Source

Beijing 2002
思路(转自http://blog.csdn.net/lyy289065406/article/details/6642599)

提示:简单的贪心

正确的算法是:要考虑把雷达站放到哪个位置使得包含雷达的区间最多!

写算法的时候要注意,按海岛的横坐标排序(纵坐标是跟随横坐标,但不能对排序构成任何影响)后,第一个雷达建立在区间的右端,然后依次判断每个区间的左端点,如果在最新建立的雷达右面,那么肯定需要一个雷达,而且也建在区间右端。如果左端点在雷达左面,这个时候要考虑区间的右端在雷达的左面还是右面,如果是右面,那雷达位置就不变,如果在左面,那现在的雷达是覆盖不了的,所以要把雷达放在该区间的右端点!因为这样同时不但能覆盖原来的岛,还能覆盖现在的岛

 

标准方法是把每个点的放置雷达的区间求出,然后按照区间排序。排好序后,从左至右看,当发现下一个区间的起始点大于前面所有区间的最小结束点的时候,答案加一。忽视前面走过的所有点后,对后面进行相同的操作(从左至右看,当发现下一个区间的起始点大于左边未被忽视的所有区间的最小结束点的时候,答案加一)。直到结束。但是这样做相对较难实现。

ac代码
#include<stdio.h>#include<math.h>#include<stdlib.h>struct s{double x,y;};int cmp(const void *a,const void *b){if((*(struct s *)a).x>(*(struct s *)b).x)return 1;elsereturn -1;}int main(){int n,ra,c=0;while(scanf("%d%d",&n,&ra)!=EOF,n||ra){int i,w=1,num=1;struct s b[10000];double l[10000],r[10000],temp;for(i=0;i<n;i++){scanf("%lf%lf",&b[i].x,&b[i].y);if(b[i].y>ra)w=0;}if(!w){printf("Case %d: -1\n",++c);continue;}qsort(b,n,sizeof(b[0]),cmp);for(i=0;i<n;i++){l[i]=b[i].x-sqrt(ra*ra-b[i].y*b[i].y);r[i]=b[i].x+sqrt(ra*ra-b[i].y*b[i].y);//printf("%lf %lf\n",b[i].x,b[i].y);}temp=r[0];for(i=1;i<n;i++){if(l[i]>temp){temp=r[i];num++;}else if(r[i]<temp)temp=r[i];}printf("Case %d: ",++c);printf("%d\n",num);}}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信网络特别慢怎么办 苹果六网速太慢怎么办 小米手机wifi网速慢怎么办 苹果8蜂窝上网慢怎么办 苹果6s4g网速慢怎么办 苹果7上网速度慢怎么办 银行转账到别人账户怎么办 银行转账转错账户怎么办 人已故欠的公款怎么办 论文抄了表格数据怎么办 电子转账转错了怎么办 苹果手机付款方式有问题怎么办 合同中付款方式错怎么办? 优步付款方式无效怎么办 工程付款方式变更没有合同怎么办 银行账号被锁了怎么办? 街电押金退不了怎么办 佣金宝账号忘了怎么办 如果汇款汇错了怎么办 手机汇款汇错了怎么办 汇款时少了数字怎么办 打过流脑后发烧怎么办 甲醛公司除完后怎么办 发票系统导出的xml 怎么办 新买的书包味道太大怎么办 alt+a截图热键冲突怎么办 白背心领发黄了怎么办 房屋装修后出现质量问题怎么办 华为p9手机音量小怎么办 华为畅享8玩游戏卡怎么办 华为畅享7玩游戏卡怎么办 华为p9升级以后屏幕失灵怎么办 荣耀9青春版玩游戏卡怎么办 华为p9屏幕不亮了怎么办 华为p9入水黑屏怎么办 农信密码忘记了怎么办 小米4g信号差怎么办 手机的调频调制器坏了怎么办 调制解调器的灯一直闪怎么办 691宽带用户名和密码无效怎么办 房间里有狐臭味怎么办