CSU1616: Heaps(区间DP)

Description

Zuosige always has bad luck. Recently, he is in hospital because of pneumonia. While he is taking his injection, he feels extremely bored. However, clever Zuosige comes up with a new game.

Zuosige knows there is a typical problem called Merging Stones. In the problem, you have N heaps of stones and you are going to merging them into one heap. The only restriction is that you can only merging adjacent heaps and the cost of a merging operation is the total number of stones in the two heaps merged. Finally, you are asked to answer the minimum cost to accomplish the merging.

However, Zuosige think this problem is too simple, so he changes it. In his problem, the cost of a merging is a polynomial function of the total number of stones in those two heaps and you are asked to answer the minimum cost.

Input

The first line contains one integer T, indicating the number of test cases.
In one test case, there are several lines.
In the first line, there are an integer N (1<=N<=1000).
In the second line, there are N integers. The i-th integer si (1<=si<=40) indicating the number of stones in the i-th heap.
In the third line, there are an integer m (1<=m<=4).
In the forth line, there are m+1 integers a0, … , am. The polynomial function is P(x)= (a0+a1*x+a2*x2+…+am*xm). (1<=ai<=5)

Output

For each test case, output an integer indicating the answer.

Sample Input

1
5
3 1 8 9 9 
2
2 1 2

Sample Output

2840

HINT

Source



题意:有n堆石头,给出每堆石头的价值,然后给出m+1个系数,合并两堆石子需要花费(a 0+a 1*x+a 2*x 2+…+a m*x m)的能量,问合并所有石子所花费的最小能量

思路:状态压缩,但是我们还需要优化,由于合并必须是相邻的,我们可以再求区间最小值的时候,同时记录合并点,然后我们枚举区间的时候,在两个合并点之间枚举,其中原因大家自己可以理解一下

include <iostream> 
#include <stdio.h> 
#include <string.h> 
#include <stack> 
#include <queue> 
#include <map> 
#include <set> 
#include <vector> 
#include <math.h> 
#include <bitset> 
#include <algorithm> 
using namespace std; 
#define ls 2*i 
#define rs 2*i+1 
#define up(i,x,y) for(i=x;i<=y;i++) 
#define down(i,x,y) for(i=x;i>=y;i--) 
#define mem(a,x) memset(a,x,sizeof(a)) 
#define w(a) while(a) 
#define LL long long 
const double pi = acos(-1.0); 
#define N 1005 
#define mod 19999997 
#define INF 0x3f3f3f3f 
#define exp 1e-8 
  
LL dp[N][N],vec[50000],sum[N]; 
int s[N],a[N],t,n,m,tot,vis[N][N]; 
  
LL col(LL x) 
{ 
    LL ans = a[0]; 
    int i,j; 
    up(i,1,m) 
    { 
        LL tem = 1; 
        up(j,1,i) tem*=x; 
        ans+=tem*a[i]; 
    } 
    return ans; 
} 
  
int main() 
{ 
    int i,j,k; 
    scanf("%d",&t); 
    w(t--) 
    { 
        scanf("%d",&n); 
        mem(sum,0); 
        mem(dp,0); 
        tot=0; 
        up(i,1,n) 
        { 
            scanf("%d",&s[i]); 
            sum[i] = sum[i-1]+s[i]; 
            tot+=s[i]; 
        } 
        scanf("%d",&m); 
        up(i,0,m) 
        { 
            scanf("%d",&a[i]); 
        } 
        up(i,1,tot) 
        { 
            vec[i]=col((LL)i); 
        } 
        up(i,1,n) vis[i][i] = i; 
        vis[0][1] = 1; 
        int len; 
        up(len,2,n) 
        { 
            up(i,1,n-len+1) 
            { 
                j = i+len-1; 
                dp[i][j] = 1LL<<60; 
                up(k,vis[i][j-1],vis[i+1][j]) 
                { 
                    LL tem = dp[i][k]+dp[k+1][j]+vec[sum[j]-sum[i-1]]; 
                    if(tem<dp[i][j]) 
                    { 
                        dp[i][j] = tem; 
                        vis[i][j] = k; //记录合并点
                    } 
                } 
            } 
        } 
        printf("%lld\n",dp[1][n]); 
    } 
  
    return 0; 
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值