hdu--5115--区间dp
..........
dp[x,y]表示 [ x , y ]这段区间所获得的价值
dp[x , y] = max( dp[x,k-1] + dp[k+1,y] + a[k] + b[x-1] + b[y+1] ) x<=k<=y
很通过上面转移方程就知道了 枚举[x,y]区间
这边 最重要的是理解 k 表示的是 最后一只要杀的狼的编号 当时我想成现在杀的狼的编号 就搞不出来了 ccccc
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 using namespace std; 3 4 int main() { 5 int T; 6 cin >> T; 7 for (int cas = 1; cas <= T; cas++) { 8 int N; 9 cin >> N; 10 int a[202], b[202]; 11 int dp[202][202]; 12 for (int i = 1; i <= N; i++) { 13 cin >> a[i]; 14 } 15 for (int i = 1; i <= N; i++) { 16 cin >> b[i]; 17 } 18 b[0] = b[N + 1] = 0; 19 for (int i = 1; i <= N; i++) { 20 dp[i][i] = a[i] + b[i - 1] + b[i + 1]; 21 } 22 for (int i = 1; i <= N ; i++) { 23 for (int j = 1; j + i <= N ; j++) { 24 dp[j][j + i] = -1; 25 for (int k = j; k <= j + i; k++) { 26 int temp; 27 if( k==j ) 28 temp = dp[k+1][j+i] + a[k] + b[j+i+1] + b[j-1]; 29 else if( k==i+j ) 30 temp = dp[j][k-1] + a[k] + b[j-1] + b[j+i+1]; 31 else 32 temp = dp[j][k-1] + dp[k+1][j + i] + a[k] + b[j-1] + b[j + i + 1]; 33 if (dp[j][j + i] == -1 || temp < dp[j][j + i] ) { 34 dp[j][j + i] = temp; 35 } 36 } 37 } 38 } 39 cout << "Case #" << cas << ": " << dp[1][N] << endl; 40 } 41 return 0; 42 }
today:
...
你找我
是习惯
吗
just follow your heart