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 ...
随机推荐
- 微软同步发行Windows 10和Windows 10 Mobile系统更新
微软今天同步公布了新的 Windows 10 Redstone PC 和 Windows 10 Mobile 预览版. PC 版本是 Build 14271.Mobile 版本是 Build 1426 ...
- ArcObject IFeature set_Shape()和Delete()报错
这样的问题主要是Ifeature实际在数据库里面不存在!可是通过IFeatureClass.getFeature()又可以得到! 详细操作流程: 首先是对要素进行删除,可是通过IFeatureClas ...
- Windows 10 优化
---恢复内容开始--- 0x00 使开始菜单,任务栏,和操作中心透明 --关闭 右下角开始菜单,选择设置,打开个性化菜单,找到颜色一栏.向下滑至最低端,使开始菜单,任务栏,和操作中心透明选项关闭 0 ...
- Verilog 浮点数运算模块
算法中常常会到浮点数运算,而浮点数的处理常常是Verilog初学中常常遇到的问题.以下将就一个简单的例子说明Verilog中浮点数运算处理. 在JPEG图像压缩时遇到色彩空间变换的问题,将YCbCr转 ...
- ANDROID L——Material Design具体解释(主题和布局)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990).谢谢支持! Android L: Google已经确认Android L就是Android Lolli ...
- java使用命令wsimport构建WebService客户端
wsimport -d d: -keep -extension -p com.demo.client http://192.168.33.3//RECEPTIONws.ASMX?WSDL 客户端:在J ...
- shiro集成encache
针对多频次或者几乎不变的大数量的数据,我们可以通过缓存来实现,具体的比如说权限认证,这个,每次操作都需要权限认证,所以,这里添加encache注解.具体的认证过程是: 1,用户第一次访问用户权限信息, ...
- JavaScript提高:002:ASP.NET使用easy UI实现tab效果
近期在做ASP.NET项目中,须要实现一个tab页控件. 发现asp.net控件中没找到现成的. 一般的实现都须要js和div配合.于是就用到了easyui里面的. 使用也非常easy.将easyui ...
- EasyUI入门视频教程
EasyUI入门视频教程02 http://www.tudou.com/programs/view/TBlaIcNU5ck/
- Spring Cloud 微服务二:API网关spring cloud zuul
前言:本章将继续上一章Spring Cloud微服务,本章主要内容是API 网关,相关代码将延续上一章,如需了解请参考:Spring Cloud 微服务一:Consul注册中心 Spring clou ...