poj1328 Radar Installation —— 贪心
题目链接:http://poj.org/problem?id=1328
题解:区间选点类的题目,求用最少的点以使得每个范围都有点存在。以每个点为圆心,r0为半径,作圆。在x轴上的弦即为雷达可放置的范围。这个范围可用勾股定理求得。记录每个点的范围,然后排序,贪心。
(由于不熟悉qsort的用法,折腾了一个小时。后来清晰了:qsort 的cmp是通过正负判断(但结构体又不是),而sort的cmp是通过对错判断,即0或1。所以qsort的cmp用‘-’(结构体除外),sort的cmp用‘<’和‘>’判断,决定以后用sort,要加上<algorithem> 和 std。
代码如下:
#include<cstdio>//poj 1328 贪心
#include<cmath>
#include<cstdlib>
#include<cstring> typedef struct
{
double l,r;
}edge, *E;
edge e[1010]; int cmp(const void *a, const void*b)
{
return ((E)a)->r > ((E)b)->r?1:-1;
} int solve(int n)
{
int sum = 1;
double brim = e[0].r;
for(int i = 1; i<n; i++)
{
if(brim<e[i].l)
{
brim = e[i].r;
sum ++;
}
}
return sum;
} int main()
{
int n,B,t = 0;
double r0,x,y,r;
while(scanf("%d %lf",&n,&r0) && (n||r0))
{
B = 1;
for(int i = 0; i<n; i++)
{
scanf("%lf %lf",&x,&y);
if(y>r0)
B = 0;
r = sqrt(r0*r0-y*y);
e[i].l = x - r;
e[i].r = x + r;
} if(B)
{
qsort(e,n,sizeof(e[0]),cmp);
printf("Case %d: %d\n",++t,solve(n));
}
else
printf("Case %d: -1\n",++t);
}
return 0;
}
poj1328 Radar Installation —— 贪心的更多相关文章
- POJ1328 Radar Installation(贪心)
题目链接. 题意: 给定一坐标系,要求将所有 x轴 上面的所有点,用圆心在 x轴, 半径为 d 的圆盖住.求最少使用圆的数量. 分析: 贪心. 首先把所有点 x 坐标排序, 对于每一个点,求出能够满足 ...
- [POJ1328]Radar Installation
[POJ1328]Radar Installation 试题描述 Assume the coasting is an infinite straight line. Land is in one si ...
- POJ--1328 Radar Installation(贪心 排序)
题目:Radar Installation 对于x轴上方的每个建筑 可以计算出x轴上一段区间可以包含这个点 所以就转化成 有多少个区间可以涵盖这所有的点 排序之后贪心一下就ok 用cin 好像一直t看 ...
- POJ1328 Radar Installation 【贪心·区间选点】
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54593 Accepted: 12 ...
- POJ 1328 Radar Installation 贪心 A
POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...
- POJ1328——Radar Installation
Radar Installation Description Assume the coasting is an infinite straight line. Land is in one side ...
- POJ1328 Radar Installation 解题报告
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- Radar Installation(贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56826 Accepted: 12 ...
- Radar Installation 贪心
Language: Default Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42 ...
随机推荐
- Tomcat Manager用户名和密码
在浏览器输入http://localhost:8080/,打开Tomcat自带的默认主页面,右侧有“administration”“documentation”等模块.选择“administratio ...
- json-path解析json方便可靠
JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPat ...
- Win7无法启动,缺少系统文件ecache.sys怎么办
网上下载ecache.sys这个文件放到System32目录下即可 http://www.wenjian.net/file/ecache.sys.html
- 【Python数据分析】魔术命令(Magic Command)
IPython有一些特殊的命令(被称为魔术命令),他们有的为常见的任务提供便利,有的则使你能够轻松的控制IPython系统的行为 魔术命令是以百分号%为前缀的命令 常用的IPython魔术命令 命令 ...
- HttpWebRequest.Proxy属性
HttpWebRequest.Proxy默认使用IE代理,可以设置“Internet选项-Internet属性-连接-局域网设置-代理服务器”.
- [BLE--Link Layer]物理信道
简述 有线通信,是用电缆直接连接.然后分距离的长短.有些会须要载入波,信号也可能会经过不同的调制方式调制. 无线通信也是一样,仅仅是信号的传输是通过射频了,通过在某一频段.对无线信道进行调制,将数据发 ...
- iOS陆哥开发笔记(七) (AVFoundation简单介绍)
在AVFoundation框架中AVAudioRecorder类专门处理录音操作,支持多种音频格式. 以下是经常使用的属性和方法: 属性 说明 @property(readonly, getter=i ...
- Spring利用propertyConfigurer类 读取.property数据库配置文件
(1).基本的使用方法是: <bean id="propertyConfigurerForAnalysis" class="org.springframework. ...
- c# .net 关于接口实现方式:隐式实现/显式实现!
以前在用到接口时,从来没注意到接口分为隐式实现与显示实现.昨天在浏览博客时看到相关内容,现在根据自己的理解记录一下,方便日后碰到的时候温习温习. 通俗的来讲,“显示接口实现”就是使用接口名称作为方法 ...
- 有一个直方图,用一个整数数组表示,其中每列的宽度为1,求所给直方图包含的最大矩形面积。比如,对于直方图[2,7,9,4],它所包含的最大矩形的面积为14(即[7,9]包涵的7x2的矩形)。给定一个直方图A及它的总宽度n,请返回最大矩形面积。保证直方图宽度小于等于500。保证结果在int范围内。
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...