poj 1328 Radar Installatio【贪心】
题目地址:http://poj.org/problem?id=1328
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://www.cnblogs.com/jackge/archive/2013/03/05/2944427.html
分析:一个岛屿坐标(x,y),在x轴上会存在一个线段区间,在这个线段区间内任意位置放置雷达都可以。
int dd=sqrt(d*d-y*y); [(double)x-dd, (double)y+dd]就是这个区间。注意区间的左右要用double类型。
当然啦如果雷达本来就覆盖不到(y>d),则是另当别论。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm> using namespace std; int n, d; struct node
{
double ll, rr;
bool operator<(const node &dd)const{
return ll<dd.ll;
}
}seg[1010]; int main()
{
int i, j;
int cnt=1;
int x, y;
bool flag; while(scanf("%d %d", &n, &d)!=EOF ){
if(n==0 && d==0 ) break; flag=true;
for(i=0; i<n; i++)
{
scanf("%d %d", &x, &y);
double dd=sqrt((double)(d*d)-y*y );
seg[i].ll = x-dd;
seg[i].rr = x+dd;
if( y>d ){ //有的岛屿位置太高, x轴上的雷达无法覆盖到 即d<当下岛屿的y
flag=false;
}
}
if(flag==false){
printf("Case %d: -1\n", cnt++); //无法解决
continue;
}
sort(seg, seg+n);
int ans=1;
node cur; cur=seg[0]; for(i=1; i<n; i++){
double li=seg[i].ll; double ri=seg[i].rr;
//一定是double类型
if( cur.rr >= ri){
cur=seg[i];
}
else if(cur.rr<li ){
ans++; cur=seg[i];
}
}
printf("Case %d: %d\n", cnt++, ans );
}
return 0;
}
poj 1328 Radar Installatio【贪心】的更多相关文章
- 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 贪心 难度: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(贪心+快排)
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 贪心算法
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- POJ 1328 Radar Installation 贪心题解
本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...
- poj 1328 Radar Installation_贪心
翻出一年多前的代码看,发现以前的代码风格很糟糕 题意:给你n个点 m为圆的半径,问需要多少个圆能把全部点圈到 #include <iostream> #include <algori ...
- POJ 1328 Radar Installation#贪心(坐标几何题)
(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<algorithm> #include<cmath ...
随机推荐
- php数组操作,内容相同,键值不同,互换
$title = array("A"=>"创建时间","C"=>"商品信息","D"=& ...
- Unity3D性能优化之Draw Call Batching
在屏幕上渲染物体,引擎需要发出一个绘制调用来访问图形API(iOS系统中为OpenGL ES).每个绘制调用需要进行大量的工作来访问图形API,从而导致了CPU方面显著的性能开销. Unity在运行时 ...
- distcc加速内核编译
Linux内核编译实在是费时间的事,搞内核移植的时候总要编译,生命有一部分就浪费在等内核编译完成上,有心想买个HP的工作站,看了下Z840的价格,想想还是算了.distcc早就听说过,一直没有去试试, ...
- [译]GLUT教程 - 鼠标
Lighthouse3d.com >> GLUT Tutorial >> Input >> The Mouse 上一节我们讨论了怎么用GLUT的键盘函数跟OpenG ...
- MVC进阶学习--View和Controller之间的数据传递(一)
1.使用ViewData ViewData 的是ControllerBase 的一个属性,是一个数据字典类型的,其实现代码如(这段代码来自asp.net MVC开源项目中源码)下: Code 1 ...
- 【数据挖掘】聚类之k-means(转载)
[数据挖掘]聚类之k-means 1.算法简述 分类是指分类器(classifier)根据已标注类别的训练集,通过训练可以对未知类别的样本进行分类.分类被称为监督学习(supervised learn ...
- Linux5_环境变量
1.总结背景 在linux系统下,下载并安装了应用程序,很有可能在键入它的名称时出现“command not found”的提示内容. 每次都到安装目标文件夹内,找到可执行文件来进行操作就太繁琐了.这 ...
- 利用freemarker生成带fusioncharts图片的word简报
/** * 利用freemarker生成带fusioncharts图片的word简报 * 烟台海颐软件技术论坛 * 作者 牟云飞 新建 * 毕业 ...
- python模块, 包的初识
Python 模块(Module), 是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代 ...
- MySql 查询列中包含数据库的关键字
MySQL查询列表中包含数据的关键字的处理办法是用``把关键字包起来(tab键上面的字符)
