题目地址: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. C++井字棋游戏,DOS界面版

    据说有一个能保证不败的算法.明天看看先再写个PVC版的. 正题.今天无聊写了个井字棋游戏,顺便逐渐让自己习惯良好的代码风格,放上来给新手学习学习. jzq2.cpp /* N字棋游戏PVP版,DOS版 ...

  2. JavaFX打包到Android上

    让JavaFX执行到移动平台一直是社区努力完毕的事.  当然,眼下已经能够让JavaFX执行到Android和IOS平台了,以下我们来看看怎样打包自己的JavaFX项目到Android平台.  首先下 ...

  3. 使用Firebug进行断点调试详解

    利用Firebug我们可以非常方便地对网页上的任何JavaScript代码进行断点调试. 首先,使用快捷键F12在当前页面打开Firebug,并切换到脚本选项卡. 其次,我们需要为指定的js代码添加断 ...

  4. Android Camera子系统之源码View

    本文基于Android 4.2.2+Linux3.6.9+SAMA5D3 SoC从源码的角度审视Android Camera子系统. 应用层 Androd原生Camera应用 /system/app/ ...

  5. Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree

    Problem A tree is a connected graph with no cycles. A rooted tree is a tree in which one special ver ...

  6. 不可忽略的apache 的 Keep Alive

    转载链接:http://hi.baidu.com/jx_iben/item/d5fe91feed74495ec9f337f1 在网页开发过程中,Keep-Alive是HTTP协议中非常重要的一个属性. ...

  7. Python修饰器的函数式编程(转)

    From:http://coolshell.cn/articles/11265.html 作者:陈皓 Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Desi ...

  8. COM线程单元

    节选自C#高级编程 不管是单线程单元还是多线程单元,一个线程只能属于一个单元. 1) 单线程单元(apartment, 寓所,套间) 单线程单元与它拥有的线程是一对一的关系.COM对象在编写时不是线程 ...

  9. C语言基础知识【基本语法】

    C 基本语法1.C 的令牌(Tokens)C 程序由各种令牌组成,令牌可以是关键字.标识符.常量.字符串值,或者是一个符号.2.分号 ;在 C 程序中,分号是语句结束符.也就是说,每个语句必须以分号结 ...

  10. java服务安装(一):使用java service wrapper及maven打zip包

    目录(?)[+] 1概述 1_1为什么要用服务形式运行 1_2如何让java程序以服务形式运行 1_3打包需求 2程序示例 3maven打zip包 3_1maven-assembly-plugin介绍 ...