1 // accepted 300 kb 0 MS 2 // interval DP 3 // DP [I] [J] indicates the smallest diaosizhi from the first entry to the J 4 // For I to J element I 5 // (1) I was the first player. diaosizhi is DP [I + 1] [J] + sum (I + 1 -- j) 6 // (2) I is not the first player, but the first K appearance, then I + 1 to K + I-1 this section of the first appearance, K + I to j k + 1 play 7 // diaoshizhi DP [I + 1] [I + k-1] + A [I] * (k-1) + (DP [I + k] [J] + K * sum (I + k -- j) 8 // sum indicates the sum of diaosizhi in a specific interval, considering K + I to J, the first appearance of K + 1 is equivalent to K + I to J, plus K * (sum (I + k -- j )) 9 # include <cstdio> 10 # include <cstring> 11 # include <iostream> 12 USI Ng namespace STD; 13 const int imax_n = 105; 14 int DP [imax_n] [imax_n]; 15 int A [imax_n], sum [imax_n]; 16 int N; 17 int min (int A, int B) 18 {19 return a <B? A: B; 20} 21 void dp () 22 {23 memset (DP, 0, sizeof (DP); 24 for (INT L = 2; L <= N; l ++) 25 {26 for (INT I = 1; I <= N; I ++) 27 {28 Int J = L-1; 29 If (j> N) break; 30 DP [I] [J] = DP [I + 1] [J] + sum [J]-sum [I]; 31 for (int K = 1; k <= L; k ++) 32 {33 DP [I] [J] = min (DP [I] [J], DP [I + 1] [I + k-1] + A [I] * (k-1) + dp [I + k] [J] + K * (sum [J]-sum [I + k-1]); 34} 35} 36} 37} 38 int main () 39 {40 int t; 41 scanf ("% d", & T); 42 for (int t = 1; T <= T; t ++) 43 {44 scanf ("% d", & N); 45 sum [0] = 0; 46 for (INT I = 1; I <= N; I ++) 47 {48 scanf ("% d", & A [I]); 49 sum [I] = sum [I-1] + A [I]; 50} 51 dp (); 52 printf ("case # % d: % d \ n", T, DP [1] [N]); 53} 54 return 0; 55}View code
1 // accepted 4792 kb 281 MS 2 // interval DP 3 // DP [I] [J] [k] I to j the minimum cost of the entire range in the K out 4 // consider the first element I in the interval, there are two situations: 5 // (1) I goes out at the K, then I + 1 to J go out at k + 1, that is, DP [I + 1] [J] [k + 1] 6 // (2) if I is not going out at K, then I will have to go out at K after I, assume that this section is from I + 1 to M 7 // DP [I + 1] [m] [k] + A [I] * (K + M-I) + dp [M + 1] [J] [K + M-I + 1] 8 # include <cstdio> 9 # include <cstring> 10 # include <iostream> 11 using namespace STD; 12 const int imax_n = 105; 13 const int INF = 100000000; 14 int DP [imax_n] [IMAX _ N] [imax_n]; 15 int A [imax_n]; 16 int N; 17 int min (int A, int B) 18 {19 return a <B? A: B; 20} 21 void dp () 22 {23 memset (DP, 0, sizeof (DP); 24 for (INT I = 1; I <= N; I ++) 25 {26 for (int K = 1; k <= N; k ++) 27 {28 DP [I] [I] [k] = (k-1) * A [I]; 29} 30} 31 for (INT I = 1; I <n; I ++) 32 {33 for (int K = 1; k <= N; k ++) 34 {35 DP [I] [I + 1] [k] = min (k-1) * A [I] + K * A [I + 1], K * A [I] + (k-1) * A [I + 1]); 36} 37} 38 for (int l = 3; L <= N; l ++) 39 {40 for (INT I = 1; I <= N; I ++) 41 {42 Int J = I + L-1; 43 If (j> N) break; 44 for (int K = 1; k <= N; k ++) 45 {46 DP [I] [J] [k] = inf; 47 DP [I] [J] [k] = min (DP [I] [J] [K], DP [I + 1] [J] [k + 1] + (k-1) * A [I]); 48 for (int m = I + 1; m <= J; M ++) 49 {50 DP [I] [J] [k] = min (DP [I] [J] [K], DP [I + 1] [m] [k] + A [I] * (K + m-i-1) + dp [M + 1] [J] [k + 1 + M-I]); 51} 52} 53} 54} 55} 56 int main () 57 {58 int t; 59 scanf ("% d", & T); 60 for (int t = 1; t <= T; t ++) 61 {62 scanf ("% d", & N); 63 for (INT I = 1; I <= N; I ++) 64 scanf ("% d ", & A [I]); 65 dp (); 66 printf ("case # % d: % d \ n", T, DP [1] [N] [1]); 67} 68 return 0; 69}View code
Hdu4283 interval DP