CSU 1616: Heaps(区间DP)
题目链接: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 ;
}
CSU 1616: Heaps(区间DP)的更多相关文章
- CSU 1592 石子合并 (经典题)【区间DP】
<题目链接> 题目大意: 现在有n堆石子,第i堆有ai个石子.现在要把这些石子合并成一堆,每次只能合并相邻两个,每次合并的代价是两堆石子的总石子数.求合并所有石子的最小代价. Input ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
随机推荐
- Java 利用SWFUpload多文件上传 session 为空失效,不能验证的问题 swfUpload多文件上传
Java 利用SWFUpload多文件上传 session 为空失效,不能验证的问题(转) 我们都知道普通的文件上传是通过表单进行文件上传的,还不能达到异步上传的目的.通过使用某些技术手段,比如jqu ...
- ♫【JS模式】偏函数
<深入浅出Node.js> var toString = Object.prototype.toString var isType = function(type) { return fu ...
- 动态规划(奇异状态):HNOI 2001 产品加工
产品加工 某加工厂有A.B两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同的机器加工同一产品所需的时间会不同,若同时由两台机器共同进行加 ...
- SendMessage API
using System; using System.IO; using System.Threading; using System.Diagnostics; using System.Runtim ...
- C语言赋值运算符
赋值运算符: 分类: 简单赋值 int a ; a=10; 复合运算符 int a ; a+=4; 复合位运算符 int a; a&=1:
- WCF扩展系列 - 行为扩展(Behaviors)
原文地址:http://www.cnblogs.com/Creator/archive/2011/05/21/2052687.html 这个系列的第一部分将会重点关注WCF行为(behaviors), ...
- [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III
Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...
- Linux内存寻址之分段机制
前言 最近在学习Linux内核,读到<深入理解Linux内核>的内存寻址一章.原本以为自己对分段分页机制已经理解了,结果发现其实是一知半解.于是,查找了很多资料,最终理顺了内存寻址的知识. ...
- poj3122
题目大意:馅饼(看起来像是一个简单点的题目啊,嘎嘎,希望是的吧) 我的生日即将来临按照习惯我将准备馅饼,不是一个馅饼,我有N块馅饼,有各种各样的味道和尺寸,当我的朋友来参加我的聚会平且他们都能得到一块 ...
- 利用组策略禁用Oultook 各个版本的缓存模式!
由于缓存模式有些自己特性和企业内的管理策略原因,有些用户不希望使用缓存模式来使用Outlook.我们怎么才能禁用缓存模式呢?请参考如下做法: 首先我们从网站上下载Office 相应的管理adm软件,这 ...