forked from Vicfred/codeforces-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vanya_and_lanterns_492B_binary_search.rs
72 lines (57 loc) · 1.48 KB
/
vanya_and_lanterns_492B_binary_search.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// https://codeforces.com/problemset/problem/492/B
// binary search
use std::io;
fn check(lanterns: &Vec<i64>, d: f64, l: i64) -> bool {
let mut current = 0;
if (lanterns[0] as f64) > d {
return false;
}
for lantern in lanterns {
let distance: f64 = f64::abs((current-*lantern) as f64);
if distance/2.0 > d {
return false;
}
current = *lantern;
}
let distance: f64 = f64::abs((current-l) as f64);
if distance > d {
return false;
}
return true;
}
fn main() {
let mut line = String::new();
io::stdin()
.read_line(&mut line)
.unwrap();
let words: Vec<i64> =
line
.split_whitespace()
.map(|x| x.parse().unwrap())
.collect();
let _n = words[0];
let l = words[1];
let mut line = String::new();
io::stdin()
.read_line(&mut line)
.unwrap();
let mut a: Vec<i64> =
line
.split_whitespace()
.map(|x| x.parse().unwrap())
.collect();
a.sort();
let mut maxima = l as f64;
let mut minima = 0.0;
let mut current = f64::abs(maxima+minima)/2.0;
for _ in 0..10000 {
current = f64::abs(maxima+minima)/2.0;
let result = check(&a, current, l);
if result {
maxima = current;
} else {
minima = current;
}
}
println!("{:.9}", current);
}