# Leetcode 813 Largest Sum of Averages （区间dp）

## Description

We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?

Note that our partition must use every number in A, and that scores are not necessarily integers.

## Input

A = [9,1,2,3,9]
K = 3


## Output

20


## 思路

$dp\left[i\right]\left[j\right]\left[k\right]$$dp[i][j][k]$ 为区间 $\left[i,j\right]$$[i,j]$ 分为 $k$$k$ 段所能得到的结果，则显然：

$dp\left[i\right]\left[j\right]\left[k\right]=max\left(dp\left[i\right]\left[j\right]\left[k\right],dp\left[i\right]\left[s\right]\left[k-1\right]+\frac{Sum\left(s+1,j\right)}{j-s}\right)$

## AC 代码

const int maxn = 1e2+10;
double dp[maxn][maxn][maxn];
int sum[maxn];
class Solution
{
public:
int getSum(int x,int y)
{
return sum[y] - (x==0?0 : sum[x-1]);
}
double largestSumOfAverages(vector<int>& A, int K)
{
memset(sum,0,sizeof(sum));
memset(dp,0,sizeof dp);
int len = A.size();
sum[0] = A[0];
for(int i=1; i<len; i++)
sum[i] = sum[i-1] + A[i];
for(int i=0; i<len; i++)
for(int j=i; j<len; j++)
{
dp[i][j][j-i+1] = getSum(i,j) * 1.0;
dp[i][j][1] = getSum(i,j) * 1.0 / (j-i+1);
}
for(int k=2; k<=K; k++)
for(int i=0; i<len; i++)
for(int j=i + k - 1; j<len; j++)
for(int s = i; s < j; s++)
dp[i][j][k] = max(dp[i][j][k],dp[i][s][k-1]+getSum(s+1,j) * 1.0 / (j-s));
return dp[0][len-1][K];
}
};