这个题的基本状态是当前这块木头需要不需要维修,第二状态是要以怎么样的形式(连续多少块)维修,一一枚举取最大值即可。值得一说的是,除了0状态(起始状态),其余都要初始化为最大值。
而第二状态的枚举需要从0开始,0代表现有的所有都全修,到i-1为止,i-1代表就修它自己(这个长度由i-j确定)。
//#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
const int INF = 0x3f3f3f3f;
using namespace std;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 2000;
inline ll lldcin()
{
ll tmp = 0, si = 1;
char c;
c = getchar();
while (c > '9' || c < '0')
{
if (c == '-')
si = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
tmp = tmp * 10 + c - '0';
c = getchar();
}
return si * tmp;
}
///Untersee Boot IXD2(1942)
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
/**Last Remote**/
ld a[5000],dp[5000];
int DETERMINATION()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
ll n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
dp[i] = INF;
dp[0] = 0;
for (int i = 1; i <= n; i++)//状态A:当前木板是否需要维修
{
if (a[i] != 0)
dp[i] = dp[i - 1];
for (int j = 0; j <i; j++)//状态B:从左边界到这里需要连续修几块木板
dp[i] = min(dp[i], dp[j] + sqrt(ld(i - j)));
}
cout << fixed<< setprecision(3) << dp[n] << endl;
return 0;
}