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. 六、angular 生成二维码

    首先需要安装angular-qrcode : bower install monospaced/angular-qrcode npm install angular-qrcode 如何使用? 在相应的 ...

  2. WCF--找不到具有绑定 BasicHttpBinding 的终结点的与方案 https 匹配的基址。注册的基址方案是 [http]。

    <system.serviceModel> <services> <service name="xxxxx.xxxxxx"> <endpo ...

  3. Django基础之Model操作

    一.数据库操作 1.创建model表 基本结构: #coding:Utf8 from django.db import models class userinfo(models.Model): #如果 ...

  4. 关闭SSL证书验证

    转载 Python3之关闭SSL证书验证 转载 Python requests 移除SSL认证,控制台输出InsecureRequestWarning取消方法 报错信息: Traceback (mos ...

  5. 为什么java中只允许继承一个类?

      一个类只能继承一个其他的类 在Java语言中禁止多重继承:一个类可以具有多个直接父类.多重继承不合法的原因是容易引发意义不明确.例如,有一个类C,如果允许它同时继承A类与B类(class C ex ...

  6. [Swift] 使用Playground

    使用Playground 1. 新建Playground 2. 写最简单的代码

  7. Linux入门-6 Linux网络基本配置

    1. 网络基础知识 网络编址 IP编址 子网掩码 同一网络主机之间通信--MAC地址 不同网络之间的通信 路由 域名 DNS 基本网络参数 2. Linux网络基础配置 以太网连接 配置网络信息 网络 ...

  8. Asp.Net MVC Identity 2.2.1 使用技巧(八)

    一.添加管理链接 在View/Shared/_layout.cshtml,在页面导航上(28行)添加如下代码: @*通过身份验证并确认用户属于Admin角色显示管理菜单*@ @if (Request. ...

  9. August 09th 2017 Week 32nd Wednesday

    Find hope from despair, life will become brilliant. 从绝望中寻找希望,人生终将辉煌. Have you ever seen the movie Ba ...

  10. 关闭window端口445

    首先,来查看下系统当前都开放了什么端口,怎样查看呢?调出cmd命令行程序,输入命令”netstat -na“,可以看到. 接着,可以发现当前系统开放了135.445以及5357端口,而且从状态看都处于 ...