1 // accepted 400 Kb 109 MS 2 // DP Linear 3 // DP [I] [J] = max (DP [I-1] [k] + A [I] [J] -K]) 4 // the maximum score of J days spent in the first I class, equal to max (K days spent in the first I-1 class + J-K days spent in the first I class) 5 # include <cstdio> 6 # include <cstring> 7 # include <iostream> 8 # include <queue> 9 # include <cmath> 10 # include <algorithm> 11 using namespace std; 12/** 13 * This is a documentation comment block14 * if you can't stick to it one day, think about why you're here! 15 * @ authr songt16 */17 const int imax_n = 105; 18 int A [imax_n] [imax_n]; 19 int DP [imax_n] [imax_n]; 20 int n, m; 21 int max (int A, int B) 22 {23 return A> B? A: B; 24} 25 void dp () 26 {27 memset (DP, 0, sizeof (DP); 28 for (INT I = 1; I <= N; I ++) 29 {30 for (Int J = 1; j <= m; j ++) 31 {32 for (int K = 0; k <= J; k ++) 33 DP [I] [J] = max (DP [I] [J], DP [I-1] [k] + A [I] [J-K]); 34} 35} 36 printf ("% d \ n ", DP [N] [m]); 37} 38 int main () 39 {40 while (scanf ("% d", & N, & M ), N + M) 41 {42 memset (A, 0, sizeof (a); 43 for (INT I = 1; I <= N; I ++) 44 For (Int J = 1; j <= m; j ++) 45 scanf ("% d", & A [I] [J]); 46 dp (); 47} 48 return 0; 49}View code
Hdu1712 linear DP