题目链接:https://www.luogu.com.cn/problem/P1005
会发现每一行之间没有影响,可以做n次区间DP。
设dp[i][j]表示区间[i,j]的取分最大值。如果从小区间向大区间转移,即
dp[i][j]=max(dp[i+1][j]+a[i],dp[i][j-1]+a[j])*2
这样$2^i$在转移的过程中便之间乘上了。
AC代码(无高精):
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 typedef long long ll; 6 const int N=90; 7 int n,m; 8 ll ans; 9 int a[N]; 10 ll f[N][N]; 11 int main(){ 12 scanf("%d%d",&n,&m); 13 for(int t=1;t<=n;t++){ 14 memset(f,0,sizeof(f)); 15 for(int i=1;i<=m;i++) scanf("%d",&a[i]); 16 for(int j=0;j<=m;j++) 17 for(int i=1;i+j<=m;i++){ 18 f[i][i+j]=max(f[i+1][i+j]+a[i],f[i][i+j-1]+a[i+j])*2; 19 } 20 ans+=f[1][m]; 21 } 22 printf("%lld",ans); 23 return 0; 24 }