1 // accepted 1784 kb 78 MS 2 // range DP 3 // DP [L1] [R1] [L2] [R2] indicates that a series from L1 to R1, the maximum score of Series B from L2 to R2 is 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}View code
Hdu4597 interval DP