Codeforces 492B B. Vanya and Lanterns
Codeforces 492B B. Vanya and Lanterns
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/B
题目:
Description
Vanya walks late at night along a straight street of length l, lit by n lanterns. Consider the coordinate system with the beginning of the street corresponding to the point 0, and its end corresponding to the point l. Then the i-th lantern is at the point ai. The lantern lights all points of the street that are at the distance of at most d from it, where d is some positive number, common for all lanterns.
Vanya wonders: what is the minimum light radius d should the lanterns have to light the whole street?
Input
The first line contains two integers n, l (1 ≤ n ≤ 1000, 1 ≤ l ≤ 109) — the number of lanterns and the length of the street respectively.
The next line contains n integers ai (0 ≤ ai ≤ l). Multiple lanterns can be located at the same point. The lanterns may be located at the ends of the street.
Output
Print the minimum light radius d, needed to light the whole street. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 9.
Sample Input
7 15 15 5 3 7 9 14 0
2.5000000000
2 5 2 5
2.0000000000
Sample Output
Hint
Consider the second sample. At d = 2 the first lantern will light the segment [0, 4] of the street, and the second lantern will light segment [3, 5]. Thus, the whole street will be lit.
题意:
N盏灯照亮长度为L的街道,使整条街全部被照亮。求灯的最少照亮半径。
分析:
1.将灯的位置排序,先求出相邻两盏灯之间的最大距离,最大距离是每盏灯所照的最大半径的两倍
2.再求第一盏灯到街首的距离和街尾到最后一盏灯的距离
3.比较三个距离的长度,最大值即为所求
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
;
int a[maxn];
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int n,l;
scanf("%d%d",&n,&l);
;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a+,a+n+); //将灯的位置从小到大排序
a[]=; //街首
a[n+]=l; //街尾
;
]-a[],xx=a[n+]-a[n]; //首尾两盏灯到街首和街尾的距离
;j<n;j++) //两盏灯之间的最大距离
{
a[j]=a[j+]-a[j];
if(a[j]>m)
m=a[j];
}
m=max(*b,max(m,*xx)); //求灯照射的最大直径
double ans=m/2.0;
printf("%.10lf\n",ans);
;
}
昨天写题目的时候没有写max函数,提交后WA了,所以这次看完题目知道要比较大小,就先写了一个max函数。
Codeforces 492B B. Vanya and Lanterns的更多相关文章
- cf492B Vanya and Lanterns
B. Vanya and Lanterns time limit per test 1 second memory limit per test 256 megabytes input standar ...
- codeforces 492B. Vanya and Lanterns 解题报告
题目链接:http://codeforces.com/problemset/problem/492/B #include <cstdio> #include <cstdlib> ...
- codeforces 677C C. Vanya and Label(组合数学+快速幂)
题目链接: C. Vanya and Label time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces 492B
Description Vanya walks late at night along a straight street of length l, lit by n lanterns. Consid ...
- Vanya and Lanterns
Description Vanya walks late at night along a straight street of length l, lit by n lanterns. Consid ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【25.33%】【codeforces 552D】Vanya and Triangles
time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【30.23%】【codeforces 552C】Vanya and Scales
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【33.33%】【codeforces 552B】Vanya and Books
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- RedHat/CentOS发行版本号及内核版本号对照表
RedHat/CentOS发行版本号及内核版本号对照表 : Redhat 9.0———————————————2.4.20-8RHEL 3 Update 8————————————2.4.21-47R ...
- PCB流程-外型加工
- 直接调用类成员函数地址(用汇编取类成员函数的地址,各VS版本还有所不同)
在C++中,成员函数的指针是个比较特殊的东西.对普通的函数指针来说,可以视为一个地址,在需要的时候可以任意转换并直接调用.但对成员函数来说,常规类型转换是通不过编译的,调用的时候也必须采用特殊的语法. ...
- Consuming Hidden WCF RIA Services
原文 http://codeseekah.com/2013/07/05/consuming-hidden-wcf-ria-services/ A Silverlight application mad ...
- 追踪神秘的成都Uber:月入2万元是现实还是传说
4月6日,一个视频在网上疯转——在上海,明星佟大为驾驶着售价近100万元的特斯拉电动汽车,作为一名Uber的司机满市转悠着拉客. Uber——优步,如果你不知道这个词,那就OUT了.就是这样的一款软件 ...
- 编译器DIY——词法分析
在上一篇文章中已经介绍了读文件的操作,那么这一篇文章中将会细致解释词法分析. 在源文件里解析出的单词流必须识别为保留字,标识符,常量,操作符和界符五大类 1.显然我们须要列举出全部的保留字,而这里与保 ...
- NSFileManager的应用
单例,是在 一个文件中只创建一次就能够全部一起共享,多创建的地址是相同的 NSFileManager *manager=[NSFileManager defaultManager]; 是一个单例 ...
- 移动端开发(四):swiper.js
swiper.js中文网:http://www.swiper.com.cn/ 文档结构 swiper.jquery.js 是需要引用jquery.js 或者 zepto.js 时,只需直接引用该 ...
- (转)原子操作 Interlocked系列函数
上一篇<多线程第一次亲密接触 CreateThread与_beginthreadex本质区别>中讲到一个多线程报数功能.为了描述方便和代码简洁起见,我们可以只输出最后的报数结果来观察程序是 ...
- Andy's First Dictionary
Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy ...