-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Solution to codechef problem vanyaAndTheLanterns
- Loading branch information
Showing
4 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Binary file not shown.
20 changes: 20 additions & 0 deletions
20
MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/vanyaAndTheLanterns.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <stdio.h> | ||
#include <algorithm> | ||
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; | ||
} |
18 changes: 18 additions & 0 deletions
18
MORE CODING PLATFORMS/Codeforces/vanyaAndTheLanterns/vanyaAndTheLanterns.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |