POJ3485 区间问题
题目描述有些坑。。
题意:
有一条高速公路在x轴上,从(0,0)到(L,0)。周围有一些村庄,希望能够在高速公路上开通几个出口,使得每个村庄到最近的出口距离小于D,求出最少需要开通多少个出口。
解题思路:
典型的区间问题,将每个点化为区间(x-sqrt(D^2-y^2),x+sqrt(D^2+y^2)),然后按区间右端点排序,依次对每个区间进行操作,如果该区间中已有出口则无需增加,若该区间没有出口则取右端点为出口。
可以证明,这是最优方案,得到最少出口数。
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#define eps 1e-6
using namespace std;
int n,ans;
double p,q,h,L,D;
struct point{
double x,y;
}a[];
bool cmp(point a,point b){
if(a.y<b.y) return ;
return ;
}
int main()
{
while(scanf("%lf%lf%d",&L,&D,&n)==){
for(int i=;i<n;i++){
scanf("%lf%lf",&p,&q);
a[i].x=p-sqrt(fabs(D*D-q*q));
a[i].y=p+sqrt(fabs(D*D-q*q));
}
sort(a,a+n,cmp);
h=-;
ans=;
for(int i=;i<n;i++)
if(!((h>a[i].x||fabs(h-a[i].x)<eps)&&(h<a[i].y||fabs(h-a[i].y<eps)))){
h=a[i].y;
if(h>L&&fabs(h-L)>eps) h=L;
ans++;
}
printf("%d",ans);
}
return ;
}
POJ3485 区间问题的更多相关文章
- ASP.NET Core应用针对静态文件请求的处理[2]: 条件请求与区间请求
通过调用ApplicationBuilder的扩展方法UseStaticFiles注册的StaticFileMiddleware中间件帮助我们处理针对文件的请求.对于StaticFileMiddlew ...
- SQL Server 随机数,随机区间,随机抽取数据rand(),floor(),ceiling(),round(),newid()函数等
在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会取随机整数.那就看下面 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
- codevs 1082 线段树区间求和
codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...
- [LeetCode] Find Right Interval 找右区间
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- [LeetCode] Non-overlapping Intervals 非重叠区间
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
随机推荐
- VBS基础篇 - 条件语句
经常地,当我们编写代码时,我们需要根据不同的判断执行不同操作,我们可以使用条件语句完成这个工作. If...Then...Else 在下面的情况中,您可以使用 If...Then...Else 语句: ...
- Java 8 VM GC Tunning Guide Charter 5
第5章 Available GC The Java HotSpot VM includes three different types of collectors, each with differe ...
- execute、executeUpdate、executeQuery三者的区别(及返回值)
1. ResultSet executeQuery(String sql); 执行SQL查询,并返回ResultSet 对象. 2.int executeUpdate(String sql); 可执行 ...
- windows最基本命令行
7:计算机运行命令全集 winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构 wupdmgr--------windows更新程序 win ...
- HDU 4811 Ball 贪心
题目链接: 题目 Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 问题描述 ...
- Java---算法---插入排序
/** * 插入排序(升序) * * @param array */ public static void insertSort(int[] array) { int j = 0; // 下标从1开始 ...
- MATLAB三维散点图的绘制(scatter3、plot3)
MATLAB三维散点图的绘制(scatter3.plot3) (1)函数scatter3 用法:scatter3(x,y,z,'.',c) % c 为颜色,需和x,y,z长度相同 例子: x=[422 ...
- Consumer Client Re-Design (翻译)
注:0.9版本Kafka的一个重大改变就是consumer和producer API的重新设计. 这篇Kafka的文档大致介绍了对于consumer API重新设计时想要实现的功能.0.9版本的确实现 ...
- DIY Ruby CPU 分析——Part I
[编者按]原文作者 Emil Soman,Rubyist,除此之外竟然同时也是艺术家,吉他手,Garden City RubyConf 组织者.本文是DIY Ruby CPU Profiling 的第 ...
- PHP中发送邮件的几种方法总结
1. 使用 mail() 函数 没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发.这个按照各个系统不同而定.使用参考手册. 2. 使用管道的形式 昨天刚测试成功,使用 ...