标签:hdu 4283 you are the one 区间dp
2 5 1 2 3 4 5 5 5 4 3 2 2
Case #1: 20 Case #2: 24
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos ( -1.0 );
const double E = 2.718281828;
typedef long long ll;
const int INF = 100000010;
using namespace std;
int a[111], n;
int dp[111][111];
int sum[111];
int main()
{
int t;
while ( cin >> t )
{
int ca = 1;
while ( t-- )
{
cin >> n;
sum[0] = 0;
for ( int i = 1; i <= n; i++ )
{
scanf ( "%d", &a[i] );
sum[i] = sum[i - 1] + a[i];
}
memset ( dp, 0, sizeof dp );
for ( int l = 1; l <= n; l++ )
{
for ( int i = 1, j = i + l - 1; j <= n; j++, i++ )
{
dp[i][j] = INF;
for ( int k = 1; k <= l; k++ )
{
dp[i][j] = min ( dp[i][j], dp[i + 1][k + i - 1] + dp[i + k][j] + k * ( sum[j] - sum[i + k - 1] ) + a[i] * ( k - 1 ) );
}
}
}
printf ( "Case #%d: %d\n", ca++, dp[1][n] );
}
}
return 0;
}
/*
2
5
1 2 3 4 5
5
5 4 3 2 2
*/
HDU 4283 You Are the One(区间dp)
标签:hdu 4283 you are the one 区间dp
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/41186909