Hdu--2014北京区域赛D
2014-11-29 16:46:55
思路:这题比赛时没看它,赛后一看直接出了方程,今天重现1A,哭成狗。。。
dp[i][j]表示处理区间[i,j]内的狼的最优解,区间型DP,然后再枚举[i,j]区间内最后杀哪只狼,dp[i][j] = max(dp[i][k - 1] + dp[k + 1][j] + b[i - 1] + b[j + 1])
1 /************************************************************************* 2 > File Name: d.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Sat 29 Nov 2014 04:19:54 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <map> 14 #include <set> 15 #include <stack> 16 #include <queue> 17 #include <iostream> 18 #include <algorithm> 19 using namespace std; 20 #define lp (p << 1) 21 #define rp (p << 1|1) 22 #define getmid(l,r) (l + (r - l) / 2) 23 #define MP(a,b) make_pair(a,b) 24 typedef long long ll; 25 const int INF = 1 << 30; 26 27 int T,N; 28 int a[210],b[210]; 29 int dp[210][210]; 30 31 int main(){ 32 scanf("%d",&T); 33 for(int tt = 1; tt <= T; ++tt){ 34 scanf("%d",&N); 35 for(int i = 1; i <= N; ++i) 36 scanf("%d",a + i); 37 for(int i = 1; i <= N; ++i) 38 scanf("%d",b + i); 39 memset(dp,0x3f,sizeof(dp)); 40 for(int i = 1; i <= N; ++i){ 41 dp[i][i] = b[i - 1] + b[i + 1]; 42 dp[i][i - 1] = dp[i + 1][i] = 0; 43 } 44 for(int len = 1; len < N; ++len){ 45 for(int i = 1; i + len <= N; ++i){ 46 int j = i + len; 47 for(int k = i; k <= j; ++k){ 48 dp[i][j] = min(dp[i][j],dp[i][k - 1] + dp[k + 1][j] + b[i - 1] + b[j + 1]); 49 } 50 } 51 } 52 int sum = 0; 53 for(int i = 1; i <= N; ++i) 54 sum += a[i]; 55 printf("Case #%d: %d\n",tt,sum + dp[1][N]); 56 } 57 return 0; 58 }