Problem:
Problem D: Tunnel

Problem

横軸$x$、縦軸$y$の$xy$平面上に$n$個の長方形がある。$i$番目の長方形は、高さ$h_i$、幅$1$であり、頂点は点$(i-1, 0)$、点$(i-1, h_i)$、点$(i, h_i)$、点$(i, 0)$である。

あなたは$n$個の正の整数$y_1, y_2, ... y_n$を選び、以下の$n$回の操作を行う。

(1) 1回目の操作では、点$(0, 1)$と点$(1, y_1)$を結ぶ線分を引く。

(2) $i$回目$(2 \le i \le n)$の操作では、点$(i-1, y_{i-1})$と点$(i, y_i)$を結ぶ線分を引く。

$i$回目の操作に対し、$S_i$を以下のように定める。

$i$回目の操作で引いた線分の$x$座標が小さい方の端点をA、$x$座標が大きい方の端点をBとする。

また、点$(i-1, 0)$をC、点$(i-1, h_i)$をD、点$(i, h_i)$をE 、点$(i, 0)$をFとする。

台形ABFCと長方形DEFCの非共通部分の面積を$S_i$とする。

以下に５つの例を示す。

それぞれの例における$S_i$は星マークを含む領域の面積の合計である。

$S = S_1 + S_2 + … + S_n$を最小化するような$y_1, y_2, ... y_n$を求め、$S$の最小値を出力せよ。

Input

入力は以下の形式で与えられる。

$n$
$h_1$ $h_2$ $...$ $h_n$

入力はすべて整数で与えられる。

Constraints

入力は以下の条件を満たす。

$2 \leq n \leq 150 $

$1 \leq h_i \leq 150 $

Output

$S$の最小値を1行に出力せよ。

ただし、$10^{−5}$ までの絶対誤差または相対誤差は許容される。

Sample Input 1

2
5 3

Sample Output 1

2.83333333

Sample Input 2

3
6 1 4

Sample Output 2

6.25000000

Sample Input 3

7
3 1 9 2 2 5 1

Sample Output 3

12.37500000

Solution in Rust (judge verdict: Accepted)

extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::f32::MAX;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut, SubAssign};
use std::collections::{BTreeMap, VecDeque, BinaryHeap, BTreeSet};
use std::fmt::{Display, Formatter, Error};

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
trait Order {
    type Output;
    fn sorted(&self) -> Self::Output;
}
impl <T: Ord + Clone> Order for Vec<T> {
    type Output = Self;
    fn sorted(&self) -> Self::Output {
        let mut result = self.clone();
        result.sort();
        result
    }
}
trait SortedVec<T> {
    fn lower_bound(&self, target: &T) -> usize;
    fn upper_bound(&self, target: &T) -> usize;
}
impl <T: Ord> SortedVec<T> for Vec<T> {
    fn lower_bound(&self, target: &T) -> usize {
        let mut left = 0;
        let mut right = self.len();
        while left < right {
            let mid = (left + right) / 2 ;
            if self[mid] < *target {
                left = mid + 1;
            }else {
                right = mid;
            }
        }
        right
    }

    fn upper_bound(&self, target: &T) -> usize {
        let mut left = 0;
        let mut right = self.len();
        while left < right {
            let mid = (left + right) / 2;
            if self[mid] <= *target {
                left = mid + 1;
            }else {
                right = mid;
            }
        }
        right
    }
}
fn solve(current: usize, height: usize, heights: &Vec<i32>, memo: &mut Vec<Vec<f64>>) -> f64 {
    if current == heights.len() {
        0_f64
    }else if memo[current][height] >= 0_f64 {
        memo[current][height]
    }else {
        let mut min_value = std::f64::MAX;
        if height as i32 >= heights[current] {
            let left = (height as i32 - heights[current]) as f64;
            for h in 1 .. heights[current] {
                let diff = left + (heights[current] - h) as f64;
                let area = (((heights[current] - h) * (heights[current] - h)) as f64 + (left * left) as f64) / diff / 2_f64;
                let all = area + solve(current + 1, h as usize, heights, memo);
                if min_value > all {
                    min_value = all;
                }
            }
            for h in heights[current] .. 301 {
                let area = (left + (h - heights[current]) as f64) / 2_f64 + solve(current + 1, h as usize, heights, memo);
                if min_value > area {
                    min_value = area;
                }
            }
        }else {
            let left = (heights[current] - height as i32) as f64;
            for h in 1 .. heights[current] {
                let area = (left + (heights[current] - h) as f64) / 2_f64 + solve(current + 1, h as usize, heights, memo);
                if min_value > area {
                    min_value = area;
                }
            }
            for h in heights[current] .. 301 {
                let diff = left + (h - heights[current]) as f64;
                let area = (((h - heights[current]) * (h - heights[current])) as f64 + left * left) / diff / 2_f64;
                let all = area + solve(current + 1, h as usize, heights, memo);
                if min_value > all {
                    min_value = all;
                }
            }
        }
        memo[current][height] = min_value;
        min_value
    }
}
fn main(){
    let n: usize = read_value!();
    let heights = read_line!(' '; i32);
    let mut memo = vec![vec![-1_f64; 301]; n];
    println!("{:.*}", 10, solve(0, 1, &heights, &mut memo));
}