Sleeping

ZZZ is an enthusiastic ACMer and he spends lots of time on training. He always stays up late for training. He needs enough time to sleep, and hates skipping classes. So he always sleeps in the class. With the final exams coming, he has to spare some time to listen to the teacher. Today, he hears that the teacher will have a revision class. The class is N (1 <= N <= 1000) minutes long. If ZZZ listens to the teacher in the i-th minute, he can get Ai points (1<=Ai<=1000). If he starts listening, he will listen to the teacher at least L (1 <= L <= N) minutes consecutively. It`s the most important that he must have at least M (1 <= M <= N) minutes for sleeping (the M minutes needn`t be consecutive). Suppose ZZZ knows the points he can get in every minute. Now help ZZZ to compute the maximal points he can get.
 
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值