zoukankan      html  css  js  c++  java
  • codeforces-492B

    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 ≤ 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.

    Examples
    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.

    从这次开始练习用c++做题,A类题会很少做了,这道题的要求是第一行输入路灯数n和路长m,第二行输入路灯所在位置,然后计算路灯照射半径是多少才能覆盖整个路面。

    路灯位置是随机的,需要排序,输出要求精确到小数点后10位,需要用到fixed和setprecision,包含在iomanip文件下。

    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    double MAX(double a, double b, double c)
    {
        if (a >= b && a >= c)
            return a;
        if (b >= c&&b >= a)
            return b;
        if (c >= a&&c >= b)
            return c;
    }
    int main()
    {
        int n, m, a[1005];
        double y=0;
        int i, j, x;
        cin >> n >> m;
    
        for (i = 0;i < n;i++)
            cin>>a[i];
        for(i=0;i<n-1;i++)
            for (j = 0;j < n - i - 1;j++)
            {
                if (a[j] < a[j + 1])
                {
                    x = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = x;
                }
            }
        for (i = 0;i < n - 1;i++)
        {
            if (a[i] - a[i + 1] > y)
                y = a[i] - a[i + 1];
        }
        y=MAX(y / 2, m - a[0], a[n - 1]);
        cout<<fixed<<setprecision(10)<<y<<endl;
        return 0;
    }
  • 相关阅读:
    [转载]Back up all of your mysql databases nightly
    编写windows7 bat运行脚本
    Windows7开通Internet信息服务
    【LeetCode题解】7_反转整数
    【LeetCode题解】350_两个数组的交集Ⅱ
    【LeetCode题解】349_两个数组的交集
    【LeetCode题解】94_二叉树的中序遍历
    【LeetCode题解】144_二叉树的前序遍历
    【LeetCode题解】2_两数相加
    【LeetCode题解】530_二分搜索树的最小绝对值差
  • 原文地址:https://www.cnblogs.com/chenruijiang/p/7874705.html
Copyright © 2011-2022 走看看