码迷,mamicode.com
首页 > 其他好文 > 详细

CSU 1616: Heaps(区间DP)优化版

时间:2015-05-10 20:35:05      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:dp

1616: Heaps

Time Limit: 2 Sec  Memory Limit: 128 MB
Submit: 31  Solved: 5
[Submit][Status][Web Board]

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

#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])//区间DP的优化
                {
                    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;
}
 
/**************************************************************
    Problem: 1616
    User: aking2015
    Language: C++
    Result: Accepted
    Time:620 ms
    Memory:13728 kb
****************************************************************/


CSU 1616: Heaps(区间DP)优化版

标签:dp

原文地址:http://blog.csdn.net/u010372095/article/details/45623927

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!