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;
    }
  • 相关阅读:
    暑假第一周总结
    洛谷P3378 【模板】堆 题解 堆(Heap)入门题
    洛谷P2170 选学霸 题解 并查集+01背包
    洛谷P1433 吃奶酪 题解 状态压缩DP
    洛谷P2835 刻录光盘 题解 点的度数+并查集
    洛谷P1991 无线通讯网 题解 并查集+二分答案
    洛谷P4185 [USACO18JAN]MooTube G 题解 并查集
    洛谷P4145 上帝造题的七分钟2 / 花神游历各国 题解 线段树+懒惰标记
    洛谷P2658 汽车拉力比赛 题解 二分答案+搜索
    洛谷P1546 最短网络 Agri-Net 题解 最小生成树/Prim算法
  • 原文地址:https://www.cnblogs.com/chenruijiang/p/7874705.html
Copyright © 2011-2022 走看看