codeforces #492B# Vanya and Lanterns(贪心)

博客探讨了如何使用贪心算法解决Codeforces #492B问题,Vanya面对一条由n个灯笼照明的直线街道,寻找照亮整条街所需的最小灯笼照明半径d。文章提供了问题描述、输入输出格式,并解释了解题思路,即计算相邻灯笼间最大距离的一半与两端灯笼位置的关系,以确定所需的最大照明半径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Vanya and Lanterns
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nl (1 ≤ n ≤ 10001 ≤ 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 test(s)
input
7 15
15 5 3 7 9 14 0
output
2.5000000000
input
2 5
2 5
output
2.0000000000
Note

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个灯笼,每个灯笼的位置固定,求最小的灯笼照明范围使得整条街都被照亮。

直接求所有相邻灯笼的距离除以2,然后在加上结合头尾两个灯笼的距离,求这些的最大值就可以了。

/********************************/
/*Problem:  codeforces #492B#   */
/*User: 	    shinelin        */
/*Memory: 	    0 KB            */
/*Time: 	    30 ms           */
/*Language: 	GNU C++         */
/********************************/
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;

#define INF 0x7fffffff
#define LL long long

vector<int> lat;
vector<double> dis;
int n, l;

int main()
{
    int x;
    scanf("%d%d", &n, &l);
    for(int i = 0; i < n; i ++)
    {
        scanf("%d", &x);
        lat.push_back(x);
    }
    sort(lat.begin(), lat.end());
    dis.push_back(lat[0]);
    dis.push_back(l-lat[n-1]);
    for(int i = 1; i < n; i ++)
    {
        dis.push_back((lat[i] - lat[i-1])/2.0);
    }
    sort(dis.begin(), dis.end());
    printf("%.9lf\n", dis[n]);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值