Radar Installation

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
 

【题目来源】

Beijing 2002

http://poj.org/problem?id=1328

【解题思路】

以每个岛的坐标为圆心画圆,会与x轴有2个交点,那么这2个点就是能覆盖该岛的雷达x 坐标区间,问题就转变成对一组区间,找最少数目的点,使得所有区间中都有一点。把包含某区间的区间删掉(如果一个点使得子区间得到满足, 那么该区间也将得到满足),这样所有区间的终止位置严格递增。

每次迭代对于第一个区间, 选择最右边一个点, 因为它可以让较多区间得到满足, 如果不选择第一个区间最右一个点(选择前面的点),
那么把它换成最右的点之后, 以前得到满足的区间, 现在仍然得到满足, 所以第一个区间的最右一个点为贪婪选择, 选择该点之后,
将得到满足的区间删掉, 进行下一步迭代, 直到结束。

ac代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAX 1010
struct Node
{
float x,y;
float l,r;
bool vis;
};
Node node[MAX];
int n;
float r;
bool cmp(Node a,Node b)
{
return a.r<b.r;
}
int main()
{
// freopen("in.txt","r",stdin);
int kase=;
while(cin>>n>>r)
{
if(n==&&r==)
break;
int i,j;
int cnt=;
if(r<=)
cnt=-;
for(i=;i<n;i++)
{
scanf("%f%f",&node[i].x,&node[i].y); //血的教训,这儿用cout绝对超时
if(node[i].y>r)
cnt=-;
node[i].l=node[i].x-sqrt(r*r-node[i].y*node[i].y);
node[i].r=node[i].x+sqrt(r*r-node[i].y*node[i].y);
}
printf("Case %d: ",kase++);
if(cnt==-)
{cout<<"-1"<<endl;continue;}
for(i=;i<n;i++)
node[i].vis=false;
sort(node,node+n,cmp);
bool flag;
for(i=;i<n&&cnt>=;i++)
{
if(!node[i].vis)
for(j=;j<n;j++)
{
if(!node[j].vis)
{
if(node[j].l<=node[i].r)
{
node[j].vis=true;
flag=true;
}
else break;
}
}
if(flag)
cnt++;
flag=;
}
cout<<cnt<<endl;
}
return ;
}

最新文章

  1. Linux CentOS中使用SQL*Plus启动和关闭数据库
  2. Cloud9:解决ThinkPHP在C9上运行时连接数据库时报错&quot;No such file or directory&quot;的问题
  3. Tomcat6性能优化
  4. editplus批量删除重复行(编辑-删除-删除重复行)
  5. 使用SparkSQL实现多线程分页查询并写入文件
  6. linux mysql数据库安装(tar.gz)
  7. SQL Server 2012 数据库各个版本功能对比
  8. PAT (Advanced Level) 1091. Acute Stroke (30)
  9. 【G】开源的分布式部署解决方案文档 - 部署Console &amp; 控制负载均衡 &amp; 跳转持续集成控制台
  10. 201521123042《Java程序设计》第11周学习总结
  11. JAVA中子类会不会继承父类的构造方法
  12. Stripe Compaction
  13. Linux(CentOS)用户修改密码有效期(chage命令)
  14. java-网络通信-索引
  15. Spring Boot 2 - 初识与新工程的创建
  16. Nginx 的 TCP 负载均衡介绍
  17. orcale三表连接查询
  18. jxl操作excel写入数据不覆盖原有数据示例
  19. IntelliJ IDEA工具的安装使用
  20. ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)

热门文章

  1. MySQL读写分离之Proxy
  2. centos7静黙安装Oracle11.2.0软件响应文件oracle_install.rsp
  3. Spring Boot Admin 2.1.0
  4. 15-C#笔记-结构体
  5. 01-赵志勇机器学习-Logistics_Regression-train
  6. 【转】编写高质量代码改善C#程序的157个建议——建议56:使用继承ISerializable接口更灵活地控制序列化过程
  7. 总结敏捷开发之Scrum
  8. Table to List&lt;object&gt; C#
  9. git blame (10)
  10. 网络协议 17 - HTTPDNS