POJ 1328 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 Installations
Input
The input is terminated by a line containing pair of zeros
Output
Sample Input
3 2
1 2
-3 1
2 1 1 2
0 2 0 0
Sample Output
Case 1: 2
Case 2: 1
Source
#include<cstdio>
#include<set>
#include<map>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
typedef long long LL;
#define MAXN 1003 /*
对于每个点可以在坐标轴上 得出能覆盖到该点的圆心范围,从而转化为x轴上的很多线段
选取一定数目的点,让所有的线段都包含至少一个点
先按结尾端点排序,然后尽量将雷达分布在右端(这样能尽可能和多个线段重叠)
*/
struct node
{
double beg,end;
}a[MAXN];
int n,d;
void cal(double x,double y,double &beg,double &end)//要求y<=d
{
double r = (double)d;
beg = x - sqrt(r*r-y*y);
end = x + sqrt(r*r-y*y);
}
bool cmp(node a,node b)
{
return a.end<b.end;
}
int main()
{
int cas = ;
while(scanf("%d%d",&n,&d),n+d)
{
double x,y;
bool f = false;
for(int i=;i<n;i++)
{
scanf("%lf%lf",&x,&y);
if(!f&&y<=d)
cal(x,y,a[i].beg,a[i].end);
else
{
f = true;
}
}
if(f)
{
printf("Case %d: -1\n",cas++);
continue;
}
sort(a,a+n,cmp);
int cnt = ;
double tmp = a[].end;
for(int i=;i<n;i++)
{
if(a[i].beg<=tmp)//因为是按结尾排序的,
//所以a[i].end肯定大于等于tmp,这种情况说明无需添加新的雷达
continue;
else//新的端点 起点无法包含,那么重新设置一个雷达(设置在新的线段最右端)
{
cnt++;
tmp = a[i].end;
}
}
printf("Case %d: %d\n",cas++,cnt);
}
return ;
}
POJ 1328 Radar Installation 贪心算法的更多相关文章
- POJ 1328 Radar Installation 贪心 A
POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...
- 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(贪心区间选点+小学平面几何)
Input The input consists of several test cases. The first line of each case contains two integers n ...
- POJ 1328 Radar Installation 贪心 难度:1
http://poj.org/problem?id=1328 思路: 1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解 2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆 ...
- poj 1328 Radar Installation(贪心)
题目:http://poj.org/problem?id=1328 题意:建立一个平面坐标,x轴上方是海洋,x轴下方是陆地.在海上有n个小岛,每个小岛看做一个点.然后在x轴上有雷达,雷达能覆盖的范 ...
- POJ 1328 Radar Installation 贪心题解
本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...
- POJ 1328 Radar Installation#贪心(坐标几何题)
(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<algorithm> #include<cmath ...
- 贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...
- poj 1328 Radar Installation (简单的贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42925 Accepted: 94 ...
随机推荐
- HDU 4691 后缀数组+RMQ
思路: 求一发后缀数组,求个LCP 就好了 注意数字有可能不只一位 (样例2) //By SiriusRen #include <bits/stdc++.h> using namespac ...
- 树莓派 关闭屏保 / RaspberryPi turn off ScreenSaver / RaspberryPi disable screen off
安装xscreensaver并配置 见:https://www.raspberrypi.org/forums/viewtopic.php?t=57552
- visual studio 2015安装
问题:安装过程老是报:安装包丢失或者损坏,但是去虚拟光驱里面可以查找到该安装包. 解决:可能文件下载ISO过程中丢失了一些数据.使用“Hash(MD5校验工具)”检测文件的“SHA-1”值,然后与官网 ...
- C#与正则表达式的例子
一个很好的文章,但是并没有测试 连接
- [ CodeForces 1059 C ] Sequence Transformation
\(\\\) \(Description\) 你现在有大小为\(N\)的一个数集,数字分别为 \(1,2,3,...N\) ,进行\(N\)轮一下操作: 输出当前数集内所有数的\(GCD\) 从数集中 ...
- Selenium 进行参数化
Selenium参数化分为大小: 小:list.dict.函数 大:txt.excel.mysql.redis 哪种方式使自己的工作简单高效就选那种!!! Selenium进行参数化有多种形式: 本文 ...
- React Native 出现红屏幕报连接服务失败
最近移动项目组在本人的带领下切换进React Native开发应用,之前没接触过,用了几周之后,发现也就那么回事吧,小玩具项目用用还是可以的.今天Android小姑娘在Windows下出问题解决不了, ...
- dedecms手机网站内页上一篇/下一篇的翻页功能
修改文件include/arc.archives.class.php文件. 1.搜索 function GetPreNext($gtype='') 2.将这个函数的所有内容替换为 function G ...
- Navicat Premium 12 破解方法
基本安装下一步下一步,破解方法参考:地址
- 6.11 将分割数据转换为多值IN列表
问题 已经有了分隔数据,想要将其转换为WHERE子句IN列表中的项目.考虑下面的字符串: 7654,7698,7782,7788 要将该字符串用在WHERE子句中,但是下面的SQL语句是错误的,因为E ...