题解 P2112 鸿雁传书
题解 P2112 鸿雁传书
题目传送门
基本思路
这其实是一道比较好理解的DP题。
我们不妨设
那么状态专题方程就可以得到为:
其中,
假设这篇文章被分为了
所以,
\texttt{std} :
#include <bits/stdc++.h>
using namespace std;
template <class type>
void read(type& a) {
char c = getchar();
type w = 1, s = 0;
while (c < '0' || c > '9') {
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
s = (s << 3) + (s << 1) + (c ^ 48);
c = getchar();
}
a = s * w;
}
const int N = 1000 + 10;
const int K = 100 + 5;
int n, k, dat[N], qzh[N];
double fc[N][K], dp[N][K];
int main()
{
read(n), read(k);
for (int i = 1; i <= n; ++i) {
string a; cin >> a;
dat[i] = a.length();
qzh[i] = qzh[i - 1] + dat[i];
}
double p = 1.0 * qzh[n] / k;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= k; ++j) dp[i][j] = 0x7fffffff;
for (int i = 1; i <= n; ++i) dp[i][1] = (qzh[i] - p) * (qzh[i] - p) / k;
for (int j = 2; j <= k; ++j)
for (int i = 1; i <= n; ++i)
for(int l = 1; l < i; ++l)
dp[i][j] = min(dp[i][j], dp[l][j - 1] + (qzh[i] - qzh[l] - p) * (qzh[i] - qzh[l] - p) * 1.0 / k);
printf("%.1lf", dp[n][k]);
return 0;
}
/*4 2
xxxx xx x xxxxx*/