poj 1328 Radar Installation(nyoj 287 Radar):贪心
点击打开链接
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 43490 | Accepted: 9640 |
Description
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
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
Sample Input
3 2
1 2
-3 1
2 1 1 2
0 2 0 0
Sample Output
Case 1: 2
Case 2: 1
题目大意就是在X轴上的点是雷达,要用最少的雷达覆盖x轴上部的点(小岛),输入小岛的个数,雷达半径和小岛的坐标,问最少需要多少雷达才能全部覆盖这些小岛
思路1:
我们反过来考虑问题,以每个小岛为圆心,雷达的覆盖范围为半径做一个圆,圆和x轴有一个或者两个交点,那么说明至少在两个交点范围内设置一个雷达才能覆盖到这个小岛,我们可以把两个交点标记为起点和终点,那么对于所有的小岛,都有一个起点和一个终点,然后我们先把这些点按起点排序,选定第一个起点和这个点对应的终点,终点设为E,然后向后选择,如果一个点的起点小于E,而且终点也小于E,那么要把E改写为当前这个点的终点;如果当前点的终点大于E,则不用修改。如果当前点的起点大于E,那么说明需要新建一个雷达以覆盖这个点,计数器加以,终点E重新设置为当前这个点的终点。
#include<stdio.h>
#include<utility>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
int num;
// freopen("test.txt", "r", stdin);
vector<pair<double, double> > v;
for(num = 1; ; num ++)
{
int n, m;
bool f = 0;
scanf("%d %d", &n, &m);
if(n == m && m == 0)
break;
int x, y;
pair<double, double> p;
double dis;
while(n--)
{
scanf("%d %d", &x, &y);
if(y > m)
{
f= 1;
}
dis = sqrt(m * m - y * y);
p.first= x - dis;
p.second = x + dis;
v.push_back(p);
}
if(f == 1)
{
printf("Case %d: -1\n", num);
v.clear();
continue;
}
sort(v.begin(), v.end());
vector<pair<double, double> > ::iterator i;
double tail = v[0].second;
int count = 1;
for(i = v.begin() + 1; i != v.end(); i++)
{
double max = tail;
bool flag = 0;
for(; (*i).first <= tail && i != v.end() ; i++)
{
flag = 1;
if((*i).second < tail)
tail = (*i).second;
}
if(i == v.end())
break; tail = (*i).second;
count ++; }
printf("Case %d: %d\n", num , count);
v.clear();
}
return 0;
}
思路2:今天重新看这个题发现思路1挺乱的,有更简单的思考方式:
以每个小岛为圆心,雷达的覆盖范围为半径做一个圆,圆和x轴有一个或者两个交点,那么说明至少在两个交点范围内设置一个雷达才能覆盖到这个小岛。我们可以把两个交点标记为起点和终点。这样我们就能算出start和end在x轴上的坐标。(这里同思路1)
不同点是:
转换一下题目的描述:
雷达越少越好 == 每个雷达覆盖的小岛数越多越好。
假设我们从左向右安装。
那么选择第一个点的时候第一个end的位置,就是在既能覆盖住第一个点,又能尽量向后覆盖的最后一个位置,这样就能保证覆盖住这个点的同时向后的覆盖面最大。覆盖面更大就更有可能覆盖住后面的点(因为还没有遍历到后面的点,所以后面的点还是未知的),从而实现雷达覆盖的小岛数越多越好这一目标
代码是在思路1的基础上改的,思路1是pair的first是start,second是end,思路二中两个变量换一下(pair自带的排序是先比较第一个)这样就能按end排序了,然后思路就是上面的了:
#include<stdio.h>
#include<utility>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
int num;
vector<pair<double, double> > v;
for(num = 1; ; num ++)
{
int n, m;
bool f = 0;
scanf("%d %d", &n, &m);
if(n == m && m == 0)
break;
int x, y;
pair<double, double> p;
double dis;
while(n--)
{
scanf("%d %d", &x, &y);
if(y > m)
{
f= 1;
}
dis = sqrt(m * m * 1.0 - y * y);
p.second= x - dis;
p.first = x + dis;
v.push_back(p);
}
if(f == 1)
{
printf("Case %d: -1\n", num);
v.clear();
continue;
}
sort(v.begin(), v.end());
vector<pair<double, double> > ::iterator i;
double tail = v[0].first;
int count = 1;
for(i = v.begin() + 1; i != v.end(); i++)
{
if((*i).second > tail)
{
count ++;
tail = (*i).first;
}
}
printf("Case %d: %d\n", num , count);
v.clear();
}
return 0;
}
poj 1328 Radar Installation(nyoj 287 Radar):贪心的更多相关文章
- 贪心问题:区间覆盖 POJ 1328 Rader Installation
题目:http://poj.org/problem?id=1328 题意:给定海岛个数,雷达半径,输入各个海岛坐标,求能覆盖所有海岛的最少雷达数 题解: 1. 贪心的区间覆盖问题,尽量让每个雷达覆盖更 ...
- 贪心 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 ...
- 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 【贪心 区间选点】
解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的 ...
- poj 1328 Radar Installation【贪心区间选点】
Radar Installation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
- Radar Installation(POJ 1328 区间贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68578 Accepted: 15 ...
- Radar Installation POJ - 1328(贪心)
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
随机推荐
- junit类找不到的问题解决
1. Class not found *******java.lang.ClassNotFoundException: ******* at java.net.URLClassLoader$1.ru ...
- jsPlumb 学习笔记
介绍 使用svg完成画图,四个概念: anchor: endpoint在的位置,可通过name访问 endpoint:connection的一端节点,通过addPoint makeSource, co ...
- 【solr】 solr 5.4.1 和tomcat 基础环境搭建
下载省略; solr下载地址:http://www.apache.org/dyn/closer.cgi/lucene/solr/ tomcat 下载安装(省略). solr5.4.1 默认在jetty ...
- [Hibernate] - one to many
事实上one to many 和 many to one是一样的,这是一个相互的过程. hibernate.cfg.xml <?xml version="1.0" encod ...
- TX Textcontrol 使用总结六——常用属性设置
1.字体设置 Tx textcontrol字体设置以版本22为例,直接设置FontSize =int,字体大小将小于正常其他控件字体设置.应做如下处理(仅供参考) this.textControl1. ...
- 【freemaker】之eclipse安装freemaker-IDE
eclipse安装插件一般我喜欢手动安装,有种可控的感觉! 首先需要下载freemaker-IDEhttp://freemarker-ide.sourceforge.net/ 下载之后解压得到 在ec ...
- 从SVN导出指定版本号之间修改的文件
当一个网站项目进入运营维护阶段以后,不会再频繁地更新全部源文件到服务器,这个时间的修改大多是局部的,因此更新文件只需更新修改过的文件,其他 没有修改过的文件就没有必要上载到服务器.但一个稍微上规模的网 ...
- Form_Form标准控件Folder开发解析(案列)
2014-01-09 Created By BaoXinjian 1. 打开APPSTAND.fmb, 并加载程序库APPFLDR.pll. 2. 基于APPSTAND.fmb生成Folder开发所需 ...
- POJ 1269 Intersecting Lines(计算几何)
题意:给定4个点的坐标,前2个点是一条线,后2个点是另一条线,求这两条线的关系,如果相交,就输出交点. 题解:先判断是否共线,我用的是叉积的性质,用了2遍就可以判断4个点是否共线了,在用斜率判断是否平 ...
- JavaScript事件基础知识总结【思维导图】
另外附上来自Nicholas C.Zakas<JavaScript高级程序设计 第3版>中的跨浏览器兼容EventUtil对象. var EventUtil = { //注册事件 addH ...