# 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


## 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];
}
};


### 2 只已被捕捉

• 不知名王垠 Chrome | 76.0.3809.132 Mac OS X

转移状态方程式 棒棒哒 谢谢|´・ω・)ノ

• 千千 Chrome | 77.0.3865.90 Windows 10

欢迎常来哦~ (☆ω☆)