hdu1244Max Sum Plus Plus Plus

跟之前做过的最大m子段一样的原理,不过这道题容易些

dp[i][j]=max(dp[i-1][j-data[i]]+sum1[j]-sum1[j-data[i]],dp[i][j-1]);第j个数是否加入第i子段,两种情况取最大就行了

#include "iostream"
#include "string.h"
#define INF 10000000;
using namespace std;
int max(int a,int b){return a>b?a:b;}
int dp[1000][1000];
int main(){
  int n,m,i,sum[1010],data[1010],num[1010],j,maxb,sum1[1010];
  while(cin>>n&&n){
    cin>>m;
    sum[0]=0;sum1[0]=0;
    for(i=1;i<=m;i++){cin>>data[i];sum[i]=sum[i-1]+data[i];dp[i][0]=0;}
    for(i=1;i<=n;i++){cin>>num[i];sum1[i]=sum1[i-1]+num[i];dp[0][i]=0;}
    //for(i=1;i<=n;i++)cout<
    dp[0][0]=0;
    maxb=-INF;
    for(i=1;i<=m;i++){
      for(j=sum[i];j<=n;j++){
         //cout<<"dp "<
         dp[i][j]=max(dp[i-1][j-data[i]]+sum1[j]-sum1[j-data[i]],dp[i][j-1]);
         maxb=max(maxb,dp[i][j]);
         //cout<<"i "<
      }
    }
    cout<endl;
  }
}

 

 

转载于:https://www.cnblogs.com/dowson/p/3279172.html

你可能感兴趣的:(hdu1244Max Sum Plus Plus Plus)