diff --git a/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/README.md b/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/README.md new file mode 100644 index 00000000..16f7955f --- /dev/null +++ b/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/README.md @@ -0,0 +1,17 @@ +# [Vanya and the lanterns](https://codeforces.com/contest/492/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 $a_i$. 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 $a_i$ ($0 ≤ a_i ≤ 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}$. + +## My solution +- sort the array of $a_i$'s in ascending order and find the biggest distance between two consecutive lamposts. Divide this by two to get $d$. +- This algorithm only works when we have lamposts at $0$ and $l$, to cover all cases we have to define diff to be $max(A[0], l- A[n-1])$ where A is our array. +- This is because to biggest difference could be between the start of the street and the the first lampost or the last lampost and the end of the street. \ No newline at end of file diff --git a/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/a.exe b/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/a.exe new file mode 100644 index 00000000..a942e289 Binary files /dev/null and b/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/a.exe differ diff --git a/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/vanyaAndTheLanterns.cpp b/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/vanyaAndTheLanterns.cpp new file mode 100644 index 00000000..57b899ff --- /dev/null +++ b/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/vanyaAndTheLanterns.cpp @@ -0,0 +1,20 @@ +#include +#include +using namespace std; + +int n, l, i, indexes[1002], diff; + +int main () +{ + scanf("%d%d", &n, &l); + for(i = 0; i < n; i++){ + scanf("%d", &indexes[i]); + } + sort(indexes, indexes+n); + diff = 2*max(indexes[0], l - indexes[n-1]); + for(i = 0; i < n -1; i++){ + diff = max(diff, indexes[i+1]- indexes[i]); + } + printf("%.10f\n", diff/2.); + return 0; +} \ No newline at end of file diff --git a/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/vanyaAndTheLanterns.py b/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/vanyaAndTheLanterns.py new file mode 100644 index 00000000..aba8fbf7 --- /dev/null +++ b/MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/vanyaAndTheLanterns.py @@ -0,0 +1,18 @@ +# 0 3 5 7 9 14 15 + + + +from operator import index + + +arr = input().split() +n = int(arr[0]) +l = int(arr[1]) +indexes = [int(i) for i in input().split()] +indexes.sort() +diff = max(indexes[0], l-indexes[-1]) +for i in range(len(indexes)-1): + a = indexes[i+1] - indexes[i] + if a > diff: + diff = a +print("{:.10f}".format(diff/2)) \ No newline at end of file