Input
The input contains several cases. The first line of each case contains three integers N, M, L mentioned in the description. The second line follows N integers separated by spaces. The i-th integer Ai means there are Ai points in the i-th minute.
Output
For each test case, output an integer, indicating the maximal points ZZZ can get.
Sample Input
10 3 3
1 2 3 4 5 6 7 8 9 10
Sample Output
49
节目分析:
用两个数组a、b
a[i][j]表示前i分钟有j分钟睡觉,且第i分钟睡觉的最大point
b[i][j]表示前i分钟有j分钟睡觉,且第i分钟不睡觉的最大point
这样可以得到状态转移方程:
a[i][j]=max(a[i-1][j-1],b[i-1][j-1]);
b[i][j]=max(b[i-1][j]+sum[i]-sum[i-1],a[i-l][j]+sum[i]-sum[i-l]);
代码:
#include <stdio.h>
#include <string.h>
int max(int x,int y)
{
return (x>y?x:y);
}
int a[1005][1005],b[1005][1005],sum[1005];
int main()
{
int i,j,k,m,l,n;
while (~scanf("%d%d%d",&n,&m,&l))
{
for (i=1;i<=n;i++)
{
scanf("%d",&sum[i]);
sum[i]+=sum[i-1];
}
int ans=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for (i=1;i<=n;i++)
{
for (j=0;j<=m&&j<=i;j++)
{
if (i-l>=j)
{
a[i][j]=max(a[i-1][j-1],b[i-1][j-1]);
b[i][j]=max(b[i-1][j]+sum[i]-sum[i-1],a[i-l][j]+sum[i]-sum[i-l]);
}
}
}
ans=max(ans,a[n][m]);
ans=max(ans,b[n][m]);
printf("%d\n",ans);
}
return 0;
}