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

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vl = vector<ll>;
template <class T> using vec = vector<T>;
template <class T> using vv = vec<vec<T>>;
template <class T> using vvv = vv<vec<T>>;
template <class T> using minpq = priority_queue<T, vector<T>, greater<T>>;
#define all(a) (a).begin(),(a).end()
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define reps(i, l, r) for(ll i = (l); i < (r); ++i)
#define rrep(i, l, r) for(ll i = (r)-1; i >= (l); --i)
#define sz(x) (ll) (x).size()
template <typename T>
bool chmax(T &a, const T& b) { return a < b ? a = b, true : false; }
template <typename T>
bool chmin(T &a, const T& b) { return a > b ? a = b, true : false; }

#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
class Point {
public:
    ld x;
    ld y;
    Point(ld _x = 0, ld _y = 0) : x(_x), y(_y) {}
    Point operator+(Point p) { return Point(x + p.x, y + p.y); }
    Point operator-(Point p) { return Point(x - p.x, y - p.y); }
    Point operator*(ld a) { return Point(a * x, a * y); }
    Point operator/(ld a) { return Point(x / a, y / a); }

    ld abs() { return sqrt(norm()); }
ld norm() { return x * x + y * y; }

    bool operator<(const Point& p) const {
        return !equals(x, p.x) ? x < p.x : y < p.y;
    }
    bool operator==(const Point& p) const {
        return fabs(x - p.x) < (1e-10) && fabs(y - p.y) < (1e-10);
    }
};
typedef Point Vector;
struct Segment {
    Point A;
    Point B;
};
typedef Segment Line;
class Circle {
public:
    Point C;
    ld r;
    Circle(Point C = Point(), ld r = 0.0) : C(C), r(r) {}
};
typedef vector<Point> Polygon;

struct Edge {
    ll from, to, cost;
    Edge (ll from, ll to, ll cost = 1ll) : from(from), to(to), cost(cost) {}
};
struct Graph {
    vector<vector<Edge>> G;
    Graph() = default;
    explicit Graph(ll N) : G(N) {}
    size_t size() const {
        return G.size();
    }
    void add(ll from, ll to, ll cost = 1ll, bool direct = 0) {
        G[from].emplace_back(from, to, cost);
        if (!direct) G[to].emplace_back(to, from, cost);
    }
    vector<Edge> &operator[](const int &k) {
        return G[k];
    }
};
using Edges = vector<Edge>;

void solve(){
    ll N; cin >> N;
    vl a(N); rep(i, N) cin >> a[i];
    auto area = [&](ll l, ll r, ll h)->ld{
        if(max(l, r) <= h){
            return (h * 2 - l - r) / 2.0;
        }
        if(min(l, r) >= h){
            return (l + r - h * 2) / 2.0;
        }
        ld re = (h - min(l, r)) * (h - min(l, r)) + (max(l, r) - h) * (max(l, r) - h);
        re /= 2 * abs(l - r);
        return re;
    };
    ll H = 301;
    vv<ld> dp(N+1, vec<ld>(H, 4e18)); dp[0][1] = 0.0;
    rep(i, N){
        reps(l, 1, H){
            reps(r, 1, H){
                chmin(dp[i+1][r], dp[i][l] + area(l, r, a[i]));
            }
        }
    }
    cout << *min_element(all(dp[N])) << endl;
}

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int t = 1;
    // cin >> t;
    while(t--){
        solve();
    }
}
accuracy: 1
time_ms: 30
memory_kb: 3864
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

#define rep(i,n) for(ll i=0;i<n;++i)
#define all(a) (a).begin(),(a).end()
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }
template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

template<typename T>
ostream &operator<<(ostream &os, const vector<T> &a){
    if (a.empty()) return os;
    os << a.front();
    for (auto e : a | views::drop(1)){
        os << ' ' << e;
    }
    return os;
}

void dump(auto ...vs){
    ((cout << vs << ' '), ...) << endl;
}



void solve() {
    ll N;
    cin>>N;
    vector<ll> H(N);
    rep(i,N)cin>>H[i];
    ll hmax=400;
    vector<ld> dp(hmax,1e18);
    dp[1]=0;
    rep(t,N){
        vector<ld> ndp(hmax,1e18);
        ll h=H[t];
        rep(i,hmax){
            if (i==0)continue;
            rep(j,hmax){
                if (j==0)continue;
                if (i<=h){
                    if (j<=h){
                        chmin(ndp[j],dp[i]+h-ld(i+j)/2);
                    }
                    else{
                        ll di=h-i;
                        ll dj=j-h;
                        chmin(ndp[j],dp[i]+ld(di*di+dj*dj)/(2*(j-i)));
                    }
                }
                else{
                    if (j>h){
                        chmin(ndp[j],dp[i]+ld(i+j)/2-h);
                    }
                    else{
                        ll di=i-h;
                        ll dj=h-j;
                        chmin(ndp[j],dp[i]+ld(di*di+dj*dj)/(2*(i-j)));
                    }
                }
            }
        }
        swap(dp,ndp);
    }
    cout<<*min_element(all(dp))<<endl;
    return;
}


int main() {
    cin.tie(0)->sync_with_stdio(0);
    ll T=1;
    while (T--){
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3840
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll ILL=2167167167167167167;
const int INF=2100000000;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> int UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,T b){if(b<a){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
bool yneos(bool a,bool upp=false){if(a){cout<<(upp?"YES\n":"Yes\n");}else{cout<<(upp?"NO\n":"No\n");}return a;}
template<class T> void vec_out(vector<T> &p,int ty=0){
    if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";}
    else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}
template<class T> T square(T a){return a * a;}


void solve();
// POP'N ROLL MUSIC / TOMOO
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;
    rep(i, 0, t) solve();
}

void solve(){
    auto f = [&](int a, int b, int h) -> double {
        if (a > b) swap(a, b);
        if (b <= h || h <= a) return (double(abs(a - h) + abs(b - h))) / 2;
        double p = (double)(h - a) / (double)(b - a);
        return (p * (h - a) + (1 - p) * (b - h)) / 2;
    };
    int N;
    cin >> N;
    vector<int> H(N);
    rep(i, 0, N) cin >> H[i], H[i]--;
    const int M = 300;
    vector<double> dp(M, INF);
    dp[0] = 0;
    rep(i, 0, N){
        vector<double> n_dp(M, INF);
        rep(a, 0, M) rep(b, 0, M) chmin(n_dp[b], f(a, b, H[i]) + dp[a]);
        swap(n_dp, dp);
    }
    cout << setprecision(20) << fixed << vec_min(dp) << "\n";
}
accuracy: 1
time_ms: 30
memory_kb: 3572
final_rank: 6

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}
//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
////////////////////////////////////////////
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> h(n);
    rep(i, 0, n)
    {
        cin >> h[i];
    }
    vector<vector<ld>> dp(n + 1, vector<ld>(301, inf));
    dp[0][1] = 0;
    rep(i, 0, n)
    {
        rep(j, 1, 301)
        {
            rep(k, 1, 301)
            {
                ld s = 0;
                ld x = abs(h[i] - j);
                ld y = abs(h[i] - k);
                if (j >= h[i] && k >= h[i])
                {
                    s = (x + y) / 2.0;
                }
                else if (j <= h[i] && k <= h[i])
                {
                    s = (x + y) / 2.0;
                }
                else
                {
                    s = (x * x + y * y) / (x + y);
                    s /= 2.0;
                }
                chmin(dp[i + 1][k], dp[i][j] + s);
            }
        }
    }
    ld ans = inf;
    rep(i, 1, 301)
    {
        chmin(ans, dp[n][i]);
    }
    cout << setprecision(15) << fixed << ans << Endl;
}
accuracy: 1
time_ms: 50
memory_kb: 4352
final_rank: 19

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}
//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
////////////////////////////////////////////
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> h(n);
    rep(i, 0, n)
    {
        cin >> h[i];
    }
    vector<vector<ld>> dp(n + 1, vector<ld>(200, inf));
    dp[0][1] = 0;
    rep(i, 0, n)
    {
        rep(j, 1, 200)
        {
            rep(k, 1, 200)
            {
                ld s = 0;
                ld x = abs(h[i] - j);
                ld y = abs(h[i] - k);
                if (j >= h[i] && k >= h[i])
                {
                    s = (x + y) / 2.0;
                }
                else if (j <= h[i] && k <= h[i])
                {
                    s = (x + y) / 2.0;
                }
                else
                {
                    s = (x * x + y * y) / (x + y) / 2.0;
                }
                chmin(dp[i + 1][k], dp[i][j] + s);
            }
        }
    }
    ld ans = inf;
    rep(i, 1, 200)
    {
        chmin(ans, dp[n][i]);
    }
    cout << setprecision(15) << fixed << ans << Endl;
}
accuracy: 0.41818181818181815
time_ms: 20
memory_kb: 4096
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;

long double H[160];
double DP[160][310];
double INF = 1e10;

double f(double a, double b, double height) {
    double la = abs(a - height);
    double lb = abs(b - height);
    if ((a - height) * (b - height) >= 0) {
        return (la + lb) / 2;
    }

    double xa = la / (la + lb);
    double xb = lb / (la + lb);
    return (xa * la + xb * lb) / 2;
}

int main() {
    int n;
    
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> H[i];
    }
    for (int i = 0; i <= 300; ++i) {
        DP[0][i] = INF;
    }
    DP[0][1] = 0;

    for (int i = 0; i < n; ++i) {
        for (int before = 1; before <= 300; ++before) {
            DP[i + 1][before] = INF;
        }
        for (int before = 1; before <= 300; ++before) {
            for (int after = 1; after <= 300; ++after) {
                DP[i + 1][after] = min(DP[i + 1][after], DP[i][before] + f(before, after, H[i]));
            }
        }
    }

    double ans = INF;
    for (int i = 1; i <= 300; ++i) {
        ans = min(ans, DP[n][i]);
    }
    cout << ans << endl;

    return 0;

}
accuracy: 1
time_ms: 20
memory_kb: 3928
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct Precision{
  Precision(){
    cout<<fixed<<setprecision(12);
  }
}precision_beet;

//INSERT ABOVE HERE
signed main(){
  int n;
  cin>>n;
  vector<int> h(n);
  for(int i=0;i<n;i++) cin>>h[i];

  const int MAX = 400;
  vector<double> dp(MAX,1e15);
  dp[1]=0;
  for(int i=0;i<n;i++){    
    vector<double> nx(MAX,1e15);
    for(int j=1;j<MAX;j++){
      for(int k=1;k<MAX;k++){
        double di=0;
        if(h[i]<=j&&h[i]<=k) di=(j-h[i])+(k-h[i]);
        else if(j<=h[i]&&k<=h[i]) di=(h[i]-j)+(h[i]-k);
        else{
          int a=h[i]-min(j,k),b=max(j,k)-h[i];
          assert(a>0&&b>0);
          di=double(a*a+b*b)/(a+b);
        }          
        chmin(nx[k],dp[j]+di/2.0);        
      }
    }    
    swap(dp,nx);
  }
  cout<<*min_element(dp.begin(),dp.end())<<endl;
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3284
final_rank: 5

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 151

int N;
double dp[NUM][2*NUM];
int height[NUM];

int main(){

	scanf("%d",&N);

	for(int i = 1; i <= N; i++){

		scanf("%d",&height[i]);
	}

	for(int i = 0; i <= N; i++){
		for(int k = 0; k <= 300; k++){
			dp[i][k] = (double)BIG_NUM;
		}
	}

	dp[0][1] = 0;

	double A,B;

	for(int i = 1; i <= N; i++){
		for(int pre_h = 1; pre_h <= 300; pre_h += 1){

			//if(fabs(dp[i-1][pre_h]-BIG_NUM) < EPS)continue;

			for(int this_h = 1; this_h <= 300; this_h += 1){

				if(height[i] >= pre_h && height[i] >= this_h){

					A = (double)(height[i]-pre_h);
					B = (double)(height[i]-this_h);

					dp[i][this_h] = min(dp[i][this_h],dp[i-1][pre_h]+(A+B)/2.0);

				}else if(height[i] >= pre_h && height[i] < this_h){

					A = (double)(height[i]-pre_h);
					B = (double)(this_h-height[i]);

					dp[i][this_h] = min(dp[i][this_h],dp[i-1][pre_h]+A*(A/(A+B))/2+B*(B/(A+B))/2);

				}else if(height[i] < pre_h && height[i] < this_h){

					A = (double)(pre_h-height[i]);
					B = (double)(this_h-height[i]);

					dp[i][this_h] = min(dp[i][this_h],dp[i-1][pre_h]+(A+B)/2.0);

				}else if(height[i] < pre_h && height[i] >= this_h){

					A = (double)(pre_h-height[i]);
					B = (double)(height[i]-this_h);

					dp[i][this_h] = min(dp[i][this_h],dp[i-1][pre_h]+A*(A/(A+B))/2+B*(B/(A+B))/2);
				}
			}
		}
	}

	double ans = BIG_NUM;

	for(int h = 1; h <= 300; h++){

		ans = min(ans,dp[N][h]);
	}

	printf("%.10lf\n",ans);

	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3532
final_rank: 14

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 151

int N;
double dp[NUM][2*NUM];
int height[NUM];

int main(){

	scanf("%d",&N);

	for(int i = 1; i <= N; i++){

		scanf("%d",&height[i]);
	}

	for(int i = 0; i <= N; i++){
		for(int k = 0; k <= 300; k++){
			dp[i][k] = (double)BIG_NUM;
		}
	}

	dp[0][1] = 0;

	double A,B;

	for(int i = 1; i <= N; i++){
		for(int pre_h = 1; pre_h <= 300; pre_h += 1){

			if(fabs(dp[i-1][pre_h]-BIG_NUM) < EPS)continue;

			for(int this_h = 1; this_h <= 300; this_h += 1){

				if(height[i] >= pre_h && height[i] >= this_h){

					A = (double)(height[i]-pre_h);
					B = (double)(height[i]-this_h);

					dp[i][this_h] = min(dp[i][this_h],dp[i-1][pre_h]+(A+B)/2.0);

				}else if(height[i] >= pre_h && height[i] < this_h){

					A = (double)(height[i]-pre_h);
					B = (double)(this_h-height[i]);

					dp[i][this_h] = min(dp[i][this_h],dp[i-1][pre_h]+A*(A/(A+B))/2+B*(B/(A+B))/2);

				}else if(height[i] < pre_h && height[i] < this_h){

					A = (double)(pre_h-height[i]);
					B = (double)(this_h-height[i]);

					dp[i][this_h] = min(dp[i][this_h],dp[i-1][pre_h]+(A+B)/2.0);

				}else if(height[i] < pre_h && height[i] >= this_h){

					A = (double)(pre_h-height[i]);
					B = (double)(height[i]-this_h);

					dp[i][this_h] = min(dp[i][this_h],dp[i-1][pre_h]+A*(A/(A+B))/2+B*(B/(A+B))/2);
				}
			}
		}
	}

	double ans = BIG_NUM;

	for(int h = 1; h <= 300; h++){

		ans = min(ans,dp[N][h]);
	}

	printf("%.10lf\n",ans);

	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3548
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#define ll long long
#define INF (1LL << 60)
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<string> vs;

const int MAX_N = 100005;

double dp[155][305];
double h[155];

double solve(double h, double l, double r)
{
    if(l <= h){
        if(r > h){
            return ((r-h)*(r-h)+(h-l)*(h-l))/(2.0*(r-l));
        }else{
            return (2*h-r-l)/2.0;
        }
    }else{
        if(r > h){
            return (r+l-2*h)/2.0;
        }else{
            return ((l-h)*(l-h)+(h-r)*(h-r))/(2.0*(l-r));
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n){
        cin >> h[i];
    }
    rep(i,n+1){
        rep(j,305){
            dp[i][j] = INF;
        }
    }
    dp[0][1] = 0;
    rep(i,n){
        srep(j,1,305){
            srep(k,1,305){
                cmn(dp[i+1][k], dp[i][j]+solve(h[i],j,k));
            }
        }
    }
    printf("%.12lf\n", *min_element(dp[n]+1, dp[n]+305));
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3624
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define r(i,n) for (int i=0; i < (n); i++)


double dp[303][303];
int n,h;

int main(){
  r(i,303)r(j,303)dp[i][j]=1e15;
  cin>>n;
  dp[0][1]=0;
  for(int i=0;i<n;i++){
    cin>>h;
    for(int j=1;j<303;j++){
      if(dp[i][j]==1e15)continue;
      for(int k=0;k<303;k++){

        if(j==k){
          if(j!=h)dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+abs(h-j) );
          else dp[i+1][k] = min( dp[i+1][k] , dp[i][j] );
        }

        if(j<k){
          if(k<=h) dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(double)abs(k-j)/2+h-k );
          else if(h<=j) dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(double)abs(k-j)/2+j-h );
          else{
            double x=(double)(h-j)/(k-j);
            dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(x*(h-j))/2+(1.0-x)*(k-h)/2 );
          }
        }

        if(j>k){
          if(j<=h) dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(double)abs(k-j)/2+h-j );
          else if(h<=k) dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(double)abs(k-j)/2+k-h );
          else{
            double x=-(double)(h-j)/(j-k);
            dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(x*(j-h))/2+(1.0-x)*(h-k)/2 );
          }
        }

      }
    }
  }

  double ans = 1e15;
  r(i,302) ans=min(ans,dp[n][i+1]);
  printf("%.9f\n",ans);
}
accuracy: 1
time_ms: 30
memory_kb: 3948
final_rank: 11

code_snippet: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, -1, 0, 0, 1, 1, -1, -1};
int dy[]={0, 0, 1, -1, 1, -1, -1, 1};

double calc(int lh, int rh, int h){
    double LH=abs(h-lh), RH=abs(h-rh);
    double S;
    
    // tow triangles
    if((h-lh>0 && h-rh<0) || (h-lh<0 && h-rh>0)){
        S=(LH/(LH+RH))*LH/2+(RH/(LH+RH))*RH/2;
    }
    else{
        S=(LH+RH)/2;
    }

    return S;
}

int main(){
    int n; cin>>n;
    vector<int> h(n);
    for(int i=0; i<n; i++) cin>>h[i];

    vector<vector<double>> dp(n+10, vector<double>(310, INF));
    for(int i=0; i<=300; i++){
        dp[1][i]=calc(1, i, h[0]);
    }

    for(int i=1; i<n; i++){
        for(int j=1; j<300; j++){ // left height
            for(int k=1; k<300; k++){ // right height
                dp[i+1][k]=min(dp[i+1][k], dp[i][j]+calc(j, k, h[i]));
            }
        }
    }

    double ans=INF;
    for(int i=1; i<300; i++){
        chmin(ans, dp[n][i]);
    }

    cout << fixed << setprecision(10) << ans << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3460
final_rank: 18

code_snippet: #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF=1e9,MOD=1e9+7;
const ll LINF=1e18;
using namespace std;
#define int long long
//template
double dp[300][300];
double frac(double h,double j,double k){
  if(h<=j&&h<=k)return (j+k)/2-h;
  else if(h>=j&&h>=k) return h-(j+k)/2;
  else if(h<j&&k<h){
    double x=j-h,y=h-k;
    return (x*x+y*y)/((x+y)*2);
  }
  else{
    double x=h-j,y=k-h;
    return (x*x+y*y)/((x+y)*2);
  }
}
//main
signed main(){
  int N;cin>>N;
  std::vector<int> v(N);
  for(int i=0;i<N;i++)cin>>v[i];
  for(int i=0;i<300;i++)for(int j=0;j<300;j++)dp[i][j]=INF;
  dp[0][1]=0;
  for(int i=1;i<=N;i++){
    for(int j=1;j<300;j++){
      for(int k=1;k<300;k++){
        dp[i][j]=min(dp[i][j],dp[i-1][k]+frac(v[i-1],j,k));
      }
    }
  }
  double ans=INF;
  for(int i=0;i<300;i++)ans=min(ans,dp[N][i]);
  cout<<fixed<<setprecision(12)<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4008
final_rank: 12

code_snippet: #include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX_N 200
#define MAX_H 400

double dp[MAX_N][MAX_H];

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int n, h[MAX_N];
	double ans = INF;
	
	cin>>n;
	
	for(int i = 1; i <= n; i++){
		cin>>h[i];
	}
	
	dp[0][1] = 0;
	
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < MAX_H; j++){
			dp[i][j] = INF;
		}
		for(int j = 1; j < MAX_H; j++){
			if(i == 1 && j != 1) continue;
			for(int k = 1; k < MAX_H; k++){
				double S;
				if((h[i]-j)*(h[i]-k) >= 0) S = (fabs(h[i]-k)+fabs(h[i]-j))/2.0;
				else S = (((double)h[i]-j)*(h[i]-j)+(double)(h[i]-k)*(h[i]-k))/(2.0*fabs(j-k));
				dp[i][k] = min(dp[i-1][j]+S,dp[i][k]);
			}
			
		}
	}

	for(int i = 0; i < MAX_H; i++){
		ans = min(dp[n][i],ans);
	}
	
	cout<<ans<<endl;
	
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3764
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
long double h[160];
double dp[160][305];

double f(double a, double b, double height) {
    double da = abs(a - height);
    double db = abs(b - height);
    if((a - height) * (b-height) >= 0) {
        return (da + db) / 2.0;
    }
    double xa = da / (da + db);
    double xb = db / (da + db);
    return (xa * da + xb * db) / 2.0;
}

int main() {
    int n;
    cin >> n;
    for(int i = 1;i <= n; i++) {
        cin >> h[i];
    }
    for(int i = 0; i <= 300; i++) {
        dp[0][i] = 1e9;
    }
    dp[0][1] = 0;
    for(int i = 1; i <= n; i++) {
        for(int after = 1; after <= 300; after++) {
            dp[i][after] = 1e9;
        }
        for(int before = 1; before <= 300; before++) {
            for(int after = 1; after <= 300; after++) {
                //cout << before << " " << after << " " << h[i] << " " << f(before, after, h[i]) << endl;
                dp[i][after] = min(dp[i][after], dp[i-1][before] + f(before, after, h[i]));
            }
        }
    }
    double ans = 1e9;
    for(int i = 1; i <= 300; i++) {
        ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3648
final_rank: 17

code_snippet: #include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f, MOD = 1000000007;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*-----------------------------------------*/
int main() {
  // freopen("input.txt", "r", stdin);

  int n; cin >> n;
  vector<int> h(n); REP(i, n) cin >> h[i];
  vector<vector<double> > dp(n, vector<double>(301, INF));
  FOR(j, 1, h[0] + 1) {
    int da = h[0] - 1, eb = h[0] - j;
    dp[0][j] = (da + eb) / 2.0;
  }
  FOR(j, h[0] + 1, 301) {
    int da = h[0] - 1, be = j - h[0];
    dp[0][j] = (da * da + be * be) / (2.0 * (da + be));
  }
  FOR(i, 1, n) {
    FOR(j, 1, h[i] + 1) {
      int eb = h[i] - j;
      FOR(k, 1, h[i] + 1) {
        int da = h[i] - k;
        dp[i][j] = min(dp[i][j], dp[i - 1][k] + (da + eb) / 2.0);
      }
      FOR(k, h[i] + 1, 301) {
        int ad = k - h[i];
        dp[i][j] = min(dp[i][j], dp[i - 1][k] + (ad * ad + eb * eb) / (2.0 * (ad + eb)));
      }
    }
    FOR(j, h[i] + 1, 301) {
      int be = j - h[i];
      FOR(k, 1, h[i] + 1) {
        int da = h[i] - k;
        dp[i][j] = min(dp[i][j], dp[i - 1][k] + (da * da + be * be) / (2.0 * (da + be)));
      }
      FOR(k, h[i] + 1, 301) {
        int ad = k - h[i];
        dp[i][j] = min(dp[i][j], dp[i][k] + (ad + be) / 2.0);
      }
    }
  }
  double ans = INF;
  FOR(j, 1, 301) ans = min(ans, dp[n - 1][j]);
  printf("%.10f\n", ans);
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3376
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

// h_iと、そのときの左のyと右のyを渡すと面積が返ってくる
double calc(int h, int y1, int y2) {
  if (y1 > h and y2 > h) {
    return fabs(0.5 * (y1 - h + y2 - h));
  }
  if (y1 <= h and y2 <= h) {
    return fabs(0.5 * (h - y1 + h - y2));
  }
  if (y1 <= h and h > y2) {
    double x = 1.0 * (h - y1) / (y2 - y1);
    return fabs(0.5 * ( x * (h - y1) + (1 - x) * (h - y2)));
  } else {
    double x = 1.0 * (h - y1) / (y2 - y1);
    return fabs(0.5 * ( x * (y1 - h) + (1 - x) * (h - y2) ));
  }

}

const double INF = 1e18;
double dp[155][333];

signed main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  // DPテーブルを初期化
  for (int i = 0; i <= n; i++) {
    for (int y = 1; y <= 300; y++) {
      dp[i][y] = INF;
    }
  }
  dp[0][1] = 0;

  // DP
  for (int i = 0; i < n; i++) {
    for (int y1 = 1; y1 <= 300; y1++) { // 左のy
      for (int y2 = 1; y2 <= 300; y2++) { // 右のy
        chmin(dp[i+1][y2], dp[i][y1] + calc(h[i], y1, y2));
      }
    }
  }

  // 最後のyはどれが最適化を選ぶ
  double ans = 1e18;
  for (int i = 1; i <= 300; i++) {
    chmin(ans, dp[n][i]);
  }

  printf("%.10f\n", ans);

  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3596
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
const double INF = 1LL<<60;

double calc(double cur, double next, double h) {
    if ((cur-h) * (next-h) >= 0) {
        return (abs(cur-h) + abs(next-h)) / 2;
    }
    else {
        double a = abs(cur - h), b = abs(next - h);
        return (a*a + b*b) / (a + b) / 2;
    }
}

int main() {
    int N; cin >> N;
    vector<double> H(N);
    for (int i = 0; i < N; ++i) cin >> H[i];

    vector<vector<double> > dp(N+1, vector<double>(300, INF));
    dp[0][1] = 0;
    for (int i = 0; i < N; ++i) {
        for (int h = 1; h < 300; ++h) {
            for (int h2 = 1; h2 < 300; ++h2) {
                dp[i+1][h2] = min(dp[i+1][h2], dp[i][h] + calc(h, h2, H[i]));
            }
        }
    }
    double res = INF;
    for (int h = 1; h < 300; ++h) res = min(res, dp[N][h]);
    cout << fixed << setprecision(10) << res << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3384
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define dbg3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define dbgB(value, size) cerr<<#value<<": "<<bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

template<typename T>
void print1(T begin, T end) {
  while (begin != end) {
    cout << (*begin) << " ";
    *begin++;
  }
  cout << endl;
}

template<typename T>
void print2(T Array, int height, int width) {
  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      cout << " " << Array[i][j];
    }
    cout << endl;
  }
}

void print() { std::cerr << endl; }
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cerr << head << " ";
  print(std::forward<Tail>(tail)...);
}

template<class T>
void Add(T &a, const T &b, const T &mod=1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) { val += mod; }
  a = val;
}

template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }

// ------------------------------------------------------------------------------------------


double calc(int h, int y1, int y2) {
  if (y1 > h and y2 > h) {
    return fabs(0.5 * (y1 - h + y2 - h));
  }
  if (y1 <= h and y2 <= h) {
    return fabs(0.5 * (h - y1 + h - y2));
  }
  if (y1 <= h and h > y2) {
    double x = 1.0 * (h - y1) / (y2 - y1);
    return fabs(0.5 * ( x * (h - y1) + (1 - x) * (h - y2)));
  } else {
    double x = 1.0 * (h - y1) / (y2 - y1);
    return fabs(0.5 * ( x * (y1 - h) + (1 - x) * (h - y2) ));
  }

}

const double INF = 1e18;
double dp[155][333];

signed main() {
  int n;
  cin >> n;
  vec<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  // 初期化
  for (int i = 0; i <= n; i++) {
    for (int y = 1; y <= 300; y++) {
      dp[i][y] = INF;
    }
  }
  dp[0][1] = 0;

  // DP
  for (int i = 0; i < n; i++) {
    for (int y1 = 1; y1 <= 300; y1++) {
      for (int y2 = 1; y2 <= 300; y2++) {
        chmin(dp[i+1][y2], dp[i][y1] + calc(h[i], y1, y2));
      }
    }
  }

  double ans = 1e18;
  for (int i = 1; i <= 300; i++) {
    chmin(ans, dp[n][i]);
  }

  // cout << ans << endl;
  printf("%.10f\n", ans);

  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3548
final_rank: 4

code_snippet: #include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(10);
	int n; cin >> n;
	vector<int> h(n); rep(i, 0, n) {
		cin >> h[i];
	}
	vector<int> y(n + 1);
	y[0] = 1;

	rep(i, 0, n) {
		y[i + 1] = h[i];
	}

	int m = *max_element(all(h)) * 2;

	vector<vector<double>> dp(n + 1, vector<double>(m, INF));

	dp[0][1] = 0;

	rep(i, 0, n) {
		rep(j, 1, m) {
			rep(k, 1, m) {
				double a = h[i] - j;
				double b = h[i] - k;
				if (a*b < 0) {
					a = abs(a), b = abs(b);
					double add = (a * a + b * b) / (a + b) / 2;
					chmin(dp[i + 1][k], dp[i][j] + add);
				}
				else {
					a = abs(a), b = abs(b);
					chmin(dp[i + 1][k], dp[i][j] + (a + b) / 2);
				}

			}
		}
	}

	dump(dp);

	cout << *min_element(all(dp[n])) << endl;

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3400
final_rank: 3