CSU 1616: Heaps(区间DP)

时间:2024-09-17 17:37:08

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1616

1616: Heaps

Time Limit: 2 Sec  Memory Limit: 128 MB
Submit:
48  Solved: 9
[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

题目大意:就是原始的石子合并的问题,相同的部分就不多介绍了,不同的便是在合并石子时,所消耗的费用不是两堆石子的总数,而是把总数代入公式:P(x)= (a0+a1*x+a2*x2+…+am*xm),同时题目也给出了a0--am的数值;

解题思路:解法就是普通的的区间DP算法,但是在做的时候老是超时,最后还是在学长的指导下,明白在第三重循环是可以不全循环,而是从上一次的两个dp中的断点之间找,并且预处理出从0到石子最大值数的带入公式的结果。唉,自己果然还是太水,改了这么久。。。。。,,看看我的备注就懂了。。心酸啊T^T
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; #define ll long long //const long long MAX=0xfffffffffffffff; ll num[],nmul[]; //nmul:储存预处理的结果
ll dp[][],mm[];
ll snum[]; //记录各数字的和
int kk[][]; //记录上一层次的断点 ll Pow(ll a,int k)
{
ll s=;
for(int i=; i<=k; i++)
s*=a;
return s;
} void Mul(int n,int m)
{
for(int i=;i<=snum[n-];i++)
{
nmul[i]=;
for(int j=;j<=m;j++)
nmul[i]+=mm[j]*Pow(i,j);
}
} int main()
{
int t,n,m;
/*for(int i=0; i<=40005; i++)
{
for(int j=0; j<=4; j++)
dd[i][j]=Pow(i,j);
}*/
scanf("%d",&t);
while(t--)
{
//snum[0]=0;
//memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(int i=; i<n; i++)
{
scanf("%lld",&num[i]);
if(i==)
snum[i]=num[i];
else
snum[i]=snum[i-]+num[i];
}
scanf("%d",&m);
for(int i=; i<=m; i++)
scanf("%lld",&mm[i]);
Mul(n,m);
for(int i=; i<n; i++)
dp[i][i]=,kk[i][i]=i;
for(int l=; l<=n; l++)
{
for(int s=; s<n-l+; s++)
{
int e=s+l-;
ll ss=1e63; //一定要定义成最大值
for(int k=kk[s][e-]; k<=kk[s+][e]; k++) //从两个断点之间找
{
if(ss>dp[s][k]+dp[k+][e])
{
ss=dp[s][k]+dp[k+][e];
kk[s][e]=k;
}
//ss=dp[s][k]+dp[k+1][e]>ss?ss:dp[s][k]+dp[k+1][e];
}
/*ll sum=0,sss=0;
for(int k=s; k<=e; k++)
sum+=num[k];
for(int k=0; k<=m; k++)
sss+=mm[k]*Pow(sum,k);*/
dp[s][e]=ss+nmul[snum[e]-snum[s-]];;
//printf("s=%d,e=%d,dp=%lld\n",s,e,dp[s][e]);
}
}
printf("%lld\n",dp[][n-]);
}
return ;
}