Thursday, June 4, 2015

Codeforces Round #280 (Div. 2) 492B Vanya and Lanterns

Solution to Codeforces Round #280 (Div. 2) 492B Vanya and Lanterns With Explanation

         Kindly comment if you have any other best solution or found any errors in my program.


Explanation:

       In this problem, you need to find the light radius R of a lantern that is the distance covered by a lantern in a direction.
       Each lantern can cover twice the radius in the street. 

Steps:

  • Get the position of lanterns in an array. 
  • Then sort this array using sort function.
  • Find the maximum distance between adjacent lanterns by walking through the sorted array and divide that max_distance by 2 to obtain the radius.
  • Now the tricky part is comparing that radius with the position of lanterns at both ends.
  • If the lanterns at both ends placed are at the exact starting and ending position of street, you can compare as it is.
  • If they are not placed at the exact starting or end position, the 1st lantern has to cover all distance before it and last lantern should cover distance after it..
  • So compare the max_distance with starting or ending position of lanterns.
  • Then print it.

        

Program:

//    Accepted                                                                                            31ms                             4KB



#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
using namespace std;

int main()
{
    int nl,l;
    int lb,rb;
    cin>>nl>>l;
    int a[nl],dia=0;
    float max_distance=0;
    for(int i=0;i<nl;i++)
        cin>>a[i];
        sort(a,a+nl);
        for(int i=1;i<nl;i++)
        {
            dia=a[i]-a[i-1];
            if(float(dia)>max_distance)
                max_distance=float(dia);

        }
        max_distance=max_distance/2;
        lb=a[0],rb=l-a[nl-1];
        if(float(lb)>max_distance)
            max_distance=float(lb);
        if(float(rb)>max_distance)
            max_distance=float(rb);
        printf("%.10f",max_distance);
        return 0;

}

No comments:

Post a Comment