1 //Accepted 1784 KB 78 ms 2 //区间dp 3 //dp[l1][r1][l2][r2] 表示a数列从l1到r1,b数列从l2到r2能得到的最大分值 4 // 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 using namespace std; 9 const int imax_n = 25; 10 int dp[imax_n][imax_n][imax_n][imax_n]; 11 int n; 12 int a[imax_n],sa[imax_n],b[imax_n],sb[imax_n]; 13 int max(int a,int b) 14 { 15 return a>b?a:b; 16 } 17 int dfs(int l1,int r1,int l2,int r2) 18 { 19 if (dp[l1][r1][l2][r2]!=-1) return dp[l1][r1][l2][r2]; 20 dp[l1][r1][l2][r2]=0; 21 if (l1<=r1) dp[l1][r1][l2][r2]=sa[r1]-sa[l1-1]+sb[r2]-sb[l2-1]-dfs(l1+1,r1,l2,r2); 22 if (l1<=r1) dp[l1][r1][l2][r2]=max(dp[l1][r1][l2][r2],sa[r1]-sa[l1-1]+sb[r2]-sb[l2-1]-dfs(l1,r1-1,l2,r2)); 23 if (l2<=r2) 24 { 25 dp[l1][r1][l2][r2]=max(dp[l1][r1][l2][r2],sa[r1]-sa[l1-1]+sb[r2]-sb[l2-1]-dfs(l1,r1,l2+1,r2)); 26 dp[l1][r1][l2][r2]=max(dp[l1][r1][l2][r2],sa[r1]-sa[l1-1]+sb[r2]-sb[l2-1]-dfs(l1,r1,l2,r2-1)); 27 } 28 return dp[l1][r1][l2][r2]; 29 } 30 int main() 31 { 32 int T; 33 scanf("%d",&T); 34 while (T--) 35 { 36 scanf("%d",&n); 37 sa[0]=0; 38 for (int i=1;i<=n;i++) 39 { 40 scanf("%d",&a[i]); 41 sa[i]=sa[i-1]+a[i]; 42 } 43 sb[0]=0; 44 for (int i=1;i<=n;i++) 45 { 46 scanf("%d",&b[i]); 47 sb[i]=sb[i-1]+b[i]; 48 } 49 memset(dp,-1,sizeof(dp)); 50 printf("%d\n",dfs(1,n,1,n)); 51 } 52 return 0; 53 }
时间: 2024-10-31 01:03:37