贪心 + 计算几何 --- Radar Installation
Radar Installation
Description
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 InstallationsInput
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
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
【题目来源】
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 ;
}
贪心 + 计算几何 --- Radar Installation的更多相关文章
- 贪心——D - Radar Installation
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- 贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...
- poj 1328 Radar Installation(贪心)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- poj 1328 Radar Installation (简单的贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42925 Accepted: 94 ...
- POJ--1328 Radar Installation(贪心 排序)
题目:Radar Installation 对于x轴上方的每个建筑 可以计算出x轴上一段区间可以包含这个点 所以就转化成 有多少个区间可以涵盖这所有的点 排序之后贪心一下就ok 用cin 好像一直t看 ...
- POJ 1328 Radar Installation 贪心 A
POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...
- 【贪心】「poj1328」Radar Installation
建模:二维转一维:贪心 Description Assume the coasting is an infinite straight line. Land is in one side of coa ...
- POJ 1328 Radar Installation 【贪心 区间选点】
解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的 ...
- Radar Installation(贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56826 Accepted: 12 ...
随机推荐
- JS树结构转list结构
树转list /** * 树转list */ function treeToList(tree){ for(var i in tree){ var node = tree[i]; list = []; ...
- php7.3升级后CI框架session失效session不能读取的问题
php7.3升级后CI框架session失效session不能读取的问题 框架ci3.0.2,php服务器从5升级到7.x后发现session失效了ci无法设置session的问题根本原因在,libr ...
- QML 缓存(QML Caching)
最近一直在学习QML,在Qt Creator中新建Qt Quick工程后,尝试不断地修改qml文件来进行试验,qml文件需要反反复复的修改. Qt Widget工程,如果修改了cpp代码后,直接构建或 ...
- Spark GraphX图算法应用【分区策略、PageRank、ConnectedComponents,TriangleCount】
一.分区策略 GraphX采用顶点分割的方式进行分布式图分区.GraphX不会沿着边划分图形,而是沿着顶点划分图形,这可以减少通信和存储的开销.从逻辑上讲,这对应于为机器分配边并允许顶点跨越多台机器. ...
- Django框架(十)--常用字段、参数、元信息、多对多关联关系
一.ORM字段 # AutoField() int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列 # IntegerField() ...
- 【Spring AOP】Spring AOP之你必须知道的AOP相关概念(1)
一.什么是AOP AOP(Aspect-oriented Programming)即面向切面编程,是对OOP( Object-oriented Programming)即面向对象编程的一种补充,AOP ...
- 《linux就该这么学》课堂笔记14 Apache、SELinux、虚拟主机
1.目前能够提供Web网络服务的程序有IIS(Windows系统中默认的Web服务程序,是一款图形化的网站管理工具).Nginx和Apache(RHEL 7系统中默认的Web服务程序)等. Apach ...
- Docker安装Redis4.0
docker pull redis:4.0 拉取Redis4.0镜像 docker images 查看本地的镜像 mkdir -p /root/redis4.0/data 在宿主机创建数据文件目录 w ...
- 使用 python set 去重 速度到底是多快呢???
这次去测试一下 python 的set去重,速度怎么样? 我们要做的是把文件一次性读取到内存中,然后去重,输出去重的长度. 第一步:对 121w 条数据去重,看去重话费的时间 上证据: 第二步:对 1 ...
- axios用法全解
[前言] 本文介绍下axios用法,希望对大家有所帮助 这里声明一句:请求数据一般放置到哪里?详见下篇文章 [主体] (1)下载 npm i axios --save (2)引入axios模块 方式1 ...