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'.

Input

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.

Output

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.

Sample Input

3 2 2
1 2 3
3 2 3
1 2 3
4 3 3
1 2 3 4

Sample Output

9
6
0

题意:

n个石子堆排成一排,每次可以将连续的[L,R]堆石子合并成一堆,花费为要合并的石子总数。求将所有石子合并成一堆的最小花费,如无法实现则输出0。

思路:

dp[i][j][k]表示将区间[i, j]合并成k堆的最小代价,转移有:

k=1时:

dp[i][j][1]=min(dp[i][j][1],dp[i][j][q]+sum[j]-sum[i-1]) 

k>1时:

dp[i][j][q]=min(dp[i][j][q],dp[i][k][q-1]+dp[k+1][j][1]) 

#include<bits/stdc++.h>
using namespace std;
#define MAX 105
#define INF 0x3f3f3f3f
int sum[MAX],dp[MAX][MAX][MAX];
int main()
{
int n,l,r,i,j,k;
while(scanf("%d%d%d",&n,&l,&r)!=EOF)
{
memset(dp,INF,sizeof(dp));
for(i=;i<=n;i++)
{
scanf("%d",&sum[i]);
dp[i][i][]=;
sum[i]+=sum[i-];
}
int len;
for(len=l;len<=r;len++) //merge长度 len[l,r]
{
for(i=;i+len-<=n;i++)//merge范围 [i,i+len-1]
{
j=i+len-;
dp[i][j][len]=;
dp[i][j][]=sum[j]-sum[i-];
}
} int q;
for(len=;len<=n;len++) //merge长度 len[2,n]
{
for(i=;i+len-<=n;i++)//merge范围 [i,i+len-1]
{
j=i+len-;
for(k=i;k<j;k++)
for(q=;q<=len;q++)
dp[i][j][q]=min(dp[i][j][q],dp[i][k][q-]+dp[k+][j][]);
for(q=l;q<=r;q++)
dp[i][j][]=min(dp[i][j][],dp[i][j][q]+sum[j]-sum[i-]);
}
} if(dp[][n][]<INF)
printf("%d\n",dp[][n][]);
else printf("0\n");
}
return ;
}

【2017 ICPC亚洲区域赛北京站 J】Pangu and Stones(区间dp)的更多相关文章

  1. 2017 ACM-ICPC亚洲区域赛北京站J题 Pangu and Stones 题解 区间DP

    题目链接:http://www.hihocoder.com/problemset/problem/1636 题目描述 在中国古代神话中,盘古是时间第一个人并且开天辟地,它从混沌中醒来并把混沌分为天地. ...

  2. 2017北京网络赛 J Pangu and Stones 区间DP(石子归并)

    #1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...

  3. 【2016 ICPC亚洲区域赛北京站 E】What a Ridiculous Election(BFS预处理)

    Description In country Light Tower, a presidential election is going on. There are two candidates,   ...

  4. 【2017 ICPC亚洲区域赛沈阳站 K】Rabbits(思维)

    Problem Description Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number li ...

  5. 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)

    队名:Unlimited Code Works(无尽编码)  队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...

  6. 2014ACM/ICPC亚洲区域赛牡丹江站汇总

    球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...

  7. icpc 2017北京 J题 Pangu and Stones 区间DP

    #1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...

  8. hihocoder 1636 : Pangu and Stones(区间dp)

    Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the first livi ...

  9. 2017 ICPC西安区域赛 A - XOR (线段树并线性基)

    链接:https://nanti.jisuanke.com/t/A1607 题面:   Consider an array AA with n elements . Each of its eleme ...

随机推荐

  1. java 中sendredirect()和forward()方法的区别

     一.文章1 HttpServletResponse.sendRedirect与RequestDispatcher.forward方法都可以实现获取相应URL资源. sendRedirect实现请求重 ...

  2. Mysql:1236常见错误

    常见的error 1236 报错一, logevent超过max_allowed_packet 大小 1. Got fatal error 1236 from master when reading ...

  3. sequelize 学习之路

    如果你觉得Sequelize的文档有点多.杂,不方便看,可以看看这篇. 在使用NodeJS来关系型操作数据库时,为了方便,通常都会选择一个合适的ORM(Object Relationship Mode ...

  4. 打通版微社区(2):服务器部署MySql数据库 For DZ3.2

    写在前面:单独写部署MySql原因是,我这边的应用数据库都是独立存在的,数据与应用分别部署在不同的服务器.另外我也没有实际部署MySql的经验,特意写一篇日志,张记性.安装MySql参考了http:/ ...

  5. Vscode rg.exe cpu 占用过高

    文件-> 首选项 -> 设置 -> 搜索search.followSymlinks 或者 修改settings.json 添加 "search.followSymlinks ...

  6. December 24th 2016 Week 52nd Saturday

    The first step is as good as half over. 第一步是最关键的一步. If one goes wrong at the first steps, what shoul ...

  7. Excel 导出指定行为txt文件(VBA,宏)

    要从Excel 多个sheet内导出指定行为txt文件,懒得用C#了,写个VBA宏 Sub Export() Dim FileName As Variant Dim Sep As String Dim ...

  8. FetchType与FetchMode的区别

    使用例: @OneToMany(mappedBy="item",cascade=CascadeType.ALL,fetch=FetchType.EAGER) @Fetch(valu ...

  9. vi编辑器的命令详情

    选定文本块,使用v复制选定块到缓冲区,使用y复制整行,用yy在同一编辑窗打开第二个文件,用:sp [filename]在多个编辑文件之间切换,用^ww剪切块,用d剪切整行用dd粘贴缓冲区中的内容,用p ...

  10. Mysql中的char与varchar length()与char_length()

    在mysql中,char和varchar都表示字符串类型.但他们存储和检索数据的方式并不相同. 在表结构定义中声明char和varchar类型时,必须指定字符串的长度.也就是该列所能存储多少个字符(不 ...