时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

In Chinese mythology, Pangu is the first living being and the creator of the sky and the earth. He woke up from an egg and split the egg into two parts: the sky and the earth.

At the beginning, there was no mountain on the earth, only stones all over the land.

There were N piles of stones, numbered from 1 to N. Pangu wanted to merge all of them into one pile to build a great mountain. If the sum of stones of some piles was S, Pangu would need S seconds to pile them into one pile, and there would be S stones in the new pile.

Unfortunately, every time Pangu could only merge successive piles into one pile. And the number of piles he merged shouldn't be less than L or greater than R.

Pangu wanted to finish this as soon as possible.

Can you help him? If there was no solution, you should answer '0'.

输入

There are multiple test cases.

The first line of each case contains three integers N,L,R as above mentioned (2<=N<=100,2<=L<=R<=N).

The second line of each case contains N integers a1,a2 …aN (1<= ai  <=1000,i= 1…N ), indicating the number of stones of  pile 1, pile 2 …pile N.

The number of test cases is less than 110 and there are at most 5 test cases in which N >= 50.

输出

For each test case, you should output the minimum time(in seconds) Pangu had to take . If it was impossible for Pangu to do his job, you should output  0.

样例输入
3 2 2
1 2 3
3 2 3
1 2 3
4 3 3
1 2 3 4
样例输出
9
6
0

题意:

n个石子堆排成一排,每次可以将连续的最少L堆,最多R堆石子合并在一起,消耗的代价为要合并的石子总数。

求合并成1堆的最小代价,如果无法做到输出0

题解:

石子归并系列题目,一般都是区间DP,于是——

dp[i][j][k] i到j 分为k堆的最小代价。显然 dp[i][j][ j-i+1]代价为0

然后[i,j] 可以划分

dp[i][j][k]  = min { dp[i][d][k-1] + dp[d+1][j][1] } (k > 1&&d-i+1 >= k-1,这个条件意思就是 区间i,d之间最少要有k-1个石子)

最后合并的时候

dp[i][j][1] = min{ dp[i][d][k-1] + dp[d+1][j][1]  + sum[j] - sum[i-1] }  (l<=k<=r)

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<utility>
#include<map>
#include<queue>
#include<set>
#define mx 0x3f3f3f3f
#define ll long long
using namespace std;
int n, l,r;
int dp[][][],sum[],a[];
//dp[i][j][t]表示区间[i,j]分为k堆的最小代价
int main()
{
while (~scanf("%d%d%d",&n,&l,&r))
{
memset(dp,0x3f,sizeof(dp));//初始化为无穷大
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];//前缀和
}
for(int i=;i<=n;i++)//初始化dp
for(int j=i;j<=n;j++)
dp[i][j][j-i+]=;//[i,j]分成j-i+1堆的代价是0
for(int len=;len<=n;len++)
{
for(int i=;i<=n-len+;i++)
{
int j=i+len-;
for(int k=i;k<j;k++)//枚举分界点
{
for(int t=l;t<=r;t++)//堆数
dp[i][j][]=min(dp[i][j][],dp[i][k][t-]+dp[k+][j][]+sum[j]-sum[i-]);
for(int t=;t<j-i+;t++)
dp[i][j][t]=min(dp[i][j][t],dp[i][k][t-]+dp[k+][j][]);
} }
}
int ans=dp[][n][];
if(ans>=mx)
printf("0\n");
else
printf("%d\n",ans); }
}

最新文章

  1. httpclient模拟post请求json封装表单数据
  2. A multi-faceted language for the Java platform
  3. ORA-01157错误,丢失undo tablespace中数据文件的解决方法
  4. Linux phpbb论坛的安装(中文版)
  5. css3彩色进度条
  6. 8月11日嵌入式Linux开发免费项目体验邀您参与
  7. android MVP模式介绍与实战
  8. css布局实践总结(part2)
  9. 【VMware虚拟化解决方案】设计和配置VMware vCenter 5.5
  10. httpclient 302 重定向
  11. ANDROID_MARS学习笔记_S01原始版_004_TableLayout
  12. IS打包
  13. ECharts JavaScript图表库 ECharts
  14. oracle序列详解
  15. js中的字符替换
  16. JFrame图形界面 ----绝对布局和按钮
  17. [Unity][安卓]Unity和Android Studio 3.0 交互通讯(1)Android Studio 3.0 设置
  18. Java当出现未被捕获的异常应该如何处理
  19. 唯美MACD
  20. Highcharts 柱图 每个柱子外围的白色边框

热门文章

  1. mac 重启php-fpm
  2. redhat 7.6 流量监控命令、软件(3)nethogs 监控进程实时流量
  3. 学习 Python,怎能不懂点PEP 呢?
  4. python - 关于json和pickle两个序列化模块的区别
  5. iOS一个简单的设置圆角不引起性能问题的分类
  6. ScrollView不设置contentSize属性依然也可以作为底层滚动View(使用masonry设置scrollView的contentSize)
  7. 区间树Splay——[NOI2005]维护数列
  8. 含有namespace的类型如何访问
  9. Atcoder Beginner Contest 139E(模拟,思维)
  10. Spring 各个组件架构