• 4thweek contest problem_B 灯笼半径问题


    题目描述如下:

    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

    Input:
    7 15 15 5 3 7 9 14 0
    Output:
    2.5000000000
    Input:
    2 5 2 5
    Output:
    2.0000000000

    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。

    代码及分析:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 const int m=1005;
     7 int point[m],len[m];
     8 
     9 int main()
    10 {
    11     int n,l,i;
    12     double r;
    13     scanf("%d%d",&n,&l);
    14     point[0]=0;     //先设置一个point[0]=0,目的仅仅是为了求len[1],后面可以忽略point[0];
    15     for(i=1;i<=n;i++)   //注意:从point[1]开始输入
    16         scanf("%d",&point[i]);
    17     sort(point+1,point+n+1);   //从point[1]到point[n]排序!!
    18     //for(i=1;i<=n;i++)
    19     // printf("%d /",point[i]);
    20     //memset(point,0,sizeof(point));
    21     
    22     for(i=1;i<=n;i++)
    23         len[i]=point[i]-point[i-1];
    24     
    25     sort(len+1,len+n+1);
    26     //for(i=1;i<=n;i++)
    27     // printf("%d  ",len[i]);
    28     double maxn=len[n];
    29     r=maxn/2.0;
    30     //printf("%llf
    ",r);
    31     double back=l-point[n];      //设置front记录第一个灯笼和街道首的距离
    32     double front=point[1]-0;     //设置back记录最后一个灯笼和街道尾的距离
    33     if(r<front||r<back)          // 特别注意此处分类
    34     {  
    35         double ans;
    36         ans=front>back?front:back;
    37         printf("%.10llf",ans);
    38     }
    39     else
    40         printf("%.10llf",r);
    41     return 0;
    42     
    43 }
  • 相关阅读:
    staticmethod & classmethod
    stanford Python
    LD_LIBRARY_PATH
    Centos7.2 errors
    theano profile
    电梯开关量GPIO配置记录
    定时器配置 中断配置 GPIO
    电梯开发进度贴
    Makefile编写学习摘要
    数据挖掘聚类算法--Kmeans
  • 原文地址:https://www.cnblogs.com/x512149882/p/4713715.html
Copyright © 2020-2023  润新知