题目地址: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【贪心】的更多相关文章

  1. POJ 1328 Radar Installation 贪心 A

    POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...

  2. POJ 1328 Radar Installation 贪心 难度:1

    http://poj.org/problem?id=1328 思路: 1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解 2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆 ...

  3. poj 1328 Radar Installation(贪心)

    题目:http://poj.org/problem?id=1328   题意:建立一个平面坐标,x轴上方是海洋,x轴下方是陆地.在海上有n个小岛,每个小岛看做一个点.然后在x轴上有雷达,雷达能覆盖的范 ...

  4. poj 1328 Radar Installation(贪心+快排)

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  5. POJ - 1328 Radar Installation(贪心区间选点+小学平面几何)

    Input The input consists of several test cases. The first line of each case contains two integers n ...

  6. POJ 1328 Radar Installation 贪心算法

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  7. POJ 1328 Radar Installation 贪心题解

    本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...

  8. poj 1328 Radar Installation_贪心

    翻出一年多前的代码看,发现以前的代码风格很糟糕 题意:给你n个点 m为圆的半径,问需要多少个圆能把全部点圈到 #include <iostream> #include <algori ...

  9. POJ 1328 Radar Installation#贪心(坐标几何题)

    (- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<algorithm> #include<cmath ...

随机推荐

  1. Ubuntu Server 命令行下显示中文乱码(菱形)解决办法

    如果Ubuntu Server在安装过程中,选择的是中文(很多新手都会在安装时选择中文,便于上手),这样在完成安装后,系统默认的语言将会是中文zh_CN.UTF- 8.但问题是我们安装的是服务器,只需 ...

  2. Mongodb基本操作入门,增删改查和索引

    主要进程 mongod.exe为启动数据库实例的进程. mongo是一个与mongod进程进行交互的JavaScript shell进程,它提供了一些交互的接口函数用户对数据库的管理. 基本命令 sh ...

  3. 进程的基本属性:进程ID、父进程ID、进程组ID、会话和控制终端

    摘要:本文主要介绍进程的基本属性,基本属性包含:进程ID.父进程ID.进程组ID.会话和控制终端. 进程基本属性 1.进程ID(PID) 函数定义:      #include <sys/typ ...

  4. js 正则表达式 取反

    http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp 以匹配中文为例 const test_value = '李钊鸿' if (/[^\u4e00 ...

  5. Android环境变量的设置(详细图解版)

    分类: Android初学学习笔记2011-07-10 09:47 99479人阅读 评论(0) 收藏 举报 androidtoolspathcmd 查阅了网上很多的资料但是对于环境变量设置介绍的不够 ...

  6. linux实用命令备忘

    1. 卸载旧内核 sudo apt-get purge linux-image-xxx-xx-generic 2. 快速换ubuntu的源: sudo sed -i 's/vivid/wily/' / ...

  7. 简洁的一键SSH脚本

    这里发一个自己图省事搞的一个批量打通SSH的脚本,可能对于好多朋友也是实用的,是expect+python的一个组合实现,原理非常easy, 使用起来也不复杂,在此还是简单贴出来说说. noscp.e ...

  8. nginx 不能解析php怎么办

    在服务器下源码安装了 mysql php  nginx,结果nginx不支持php.解决方法,在nginx配置文件中添加: ocation ~ .*\.php?$ { fastcgi_pass 127 ...

  9. (转)linux设备驱动之USB数据传输分析 一

    三:传输过程的实现说到传输过程,我们必须要从URB开始说起,这个结构的就好比是网络子系统中的skb,好比是I/O中的bio.USB系统的信息传输就是打成URB结构,然后再过行传送的.URB的全称叫US ...

  10. [转]Html position(static、relative、absolute、fixed)

    转自:http://blog.csdn.net/topviewers/article/details/21644305 讲解不错,转载备忘. position的四个属性值: 1.relative2.a ...