首页 > 代码库 > hdu 1244 DP

hdu 1244 DP

水DP

 dp[i%2][j]=Max(dp[i%2][j-1],dp[1-i%2][j-l[i]]+sum[j]-sum[j-l[i]]);


#include "stdio.h"
#include "string.h"
int Max(int a,int b)
{
    if (a<b) return b;
    else return a;
}
int dp[2][1010];
int a[1010],sum[1010],l[22];
int main()
{
    int n,m,i,ans,j;

    while (scanf("%d",&n)!=EOF)
    {
        if (n==0) break;
        scanf("%d",&m);
        for (i=1;i<=m;i++)
            scanf("%d",&l[i]);
        sum[0]=0;
        for (i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        memset(dp,0,sizeof(dp));

        ans=0;
        for (i=1;i<=m;i++)
            for (j=l[i];j<=n;j++)
            {
                dp[i%2][j]=dp[i%2][j-1];
                dp[i%2][j]=Max(dp[i%2][j],dp[1-i%2][j-l[i]]+sum[j]-sum[j-l[i]]);

                ans=Max(dp[i%2][j],ans);
            }
        printf("%d\n",ans);
    }
    return 0;
}


hdu 1244 DP