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<<sum1[] dp[0][0]=0; maxb=-INF; for(i=1;i<=m;i++){ for(j=sum[i];j<=n;j++){ //cout<<"dp "<<dp[i][j]<<' '<<dp[i-1][j-data[i]]+sum1[j]-sum1[j-data[i]]<<endl; 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 "<<i<<" j "<<j<<" dp[j-data[i]] "<<dp[i-1][j-data[i]]<<" sum1 "<<sum1[j]<<' '<<sum1[j-data[i]]<<" dp[j] "<<dp[i][j]<<endl; } } cout<<maxb<<endl; } }