Pangu and Stones

时间限制: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堆最少需要花费多少时间

题解

dp[i][j][k]表示i~j这个区间合成k堆所需要的最小时间,故可得状态转移方程式:
d为枚举的区间间隔
1.k==1 dp[i][i+d][1]=min(dp[i][i+d][1],dp[i][j][k]+dp[j+1][i+d][1]+sum[i][i+d])
(l-1<=k<=r-1)
2.k>=2 dp[i][i+d][k]=min(dp[i][i+d][k],dp[i][j][k-1]+dp[j+1][i+d][1])
此处k不用做限制

事实上,只需要在合并一堆的时候限制条件就行了,因为所有k>2的情况都是由k=1的情况得出的,所以在都初始化为inf的情况下,不能合成1堆,dp[i][j][1]=inf,那么后面所有由dp[i][j][1]推出的情况也是inf

C++代码

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=;
const int inf=0x3f3f3f3f;
int n,l,r;
int w[N];
int sum[N][N];
int dp[N][N][N];//i~j区间分成k堆最小价格
int main()
{
while(~scanf("%d%d%d",&n,&l,&r))
{
for(int i=; i<=n; i++)
scanf("%d",&w[i]);
mem(dp,inf);
for(int i=; i<=n; i++)
{
sum[i][i-]=;
for(int j=i; j<=n; j++)
{
sum[i][j]=sum[i][j-]+w[j];
dp[i][j][j-i+]=;//初始化初状态
}
}
for(int d=; d<=n; d++)
for(int i=; i+d<=n; i++)
{
for(int j=i; j<=i+d-; j++)
for(int k=l-; k<=r-; k++)
{
dp[i][i+d][]=min(dp[i][i+d][],dp[i][j][k]+dp[j+][i+d][]+sum[i][i+d]);
}
for(int k=; k<=d; k++)
for(int j=i; j<=i+d-; j++)
dp[i][i+d][k]=min(dp[i][i+d][k],dp[i][j][k-]+dp[j+][i+d][]);
}
if(dp[][n][]==inf)
puts("");
else
printf("%d\n",dp[][n][]);
}
return ;
}

hihocoder 1636 : Pangu and Stones(区间dp)的更多相关文章

  1. hihoCoder 1636 Pangu and Stones

    hihoCoder 1636 Pangu and Stones 思路:区间dp. 状态:dp[i][j][k]表示i到j区间合并成k堆石子所需的最小花费. 初始状态:dp[i][j][j-i+1]=0 ...

  2. [ICPC 北京 2017 J题]HihoCoder 1636 Pangu and Stones

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

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

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

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

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

  5. HihoCoder - 1636 Pangu and Stones(区间DP)

    有n堆石子,每次你可以把相邻的最少L堆,最多R堆合并成一堆. 问把所有石子合并成一堆石子的最少花费是多少. 如果不能合并,输出0. 石子合并的变种问题. 用dp[l][r][k]表示将 l 到 r 之 ...

  6. HihoCoder 1636 Pangu and Stones(区间DP)题解

    题意:合并石子,每次只能合并l~r堆成1堆,代价是新石堆石子个数,问最后能不能合成1堆,不能输出0,能输出最小代价 思路:dp[l][r][t]表示把l到r的石堆合并成t需要的最小代价. 当t == ...

  7. 2017ICPC北京 J:Pangu and Stones

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

  8. Pangu and Stones HihoCoder - 1636 区间DP

    Pangu and Stones HihoCoder - 1636 题意 给你\(n\)堆石子,每次只能合成\(x\)堆石子\((x\in[L, R])\),问把所有石子合成一堆的最小花费. 思路 和 ...

  9. Pangu and Stones(HihoCoder-1636)(17北京OL)【区间DP】

    题意:有n堆石头,盘古每次可以选择连续的x堆合并,所需时间为x堆石头的数量之和,x∈[l,r],现在要求,能否将石头合并成一堆,如果能,最短时间是多少. 思路:(参考了ACM算法日常)DP[i][j] ...

随机推荐

  1. linux 内存

    [转]Linux 查看内存(free buffer cache) 转自:http://elf8848.iteye.com/blog/1995638 Linux下如何查内存信息,如内存总量.已使用量.可 ...

  2. JUnit——assertThat(acture,matcher)

    使用hamcrest之前需要引入相关的jar包,包括hamcrest-core.1.3.jar和hamcrest-library-1.3.jar. 具体引入的方法为:右击JUnit工程——build ...

  3. Oracle报Ora01522

    应用服务报错截图 数据库后台日志报错截图 从日志分析应该是回滚异常造成表空间无法使用回滚段,而回滚涉及的表空间为undo表空间 尝试新建UNDO表空间,再将UNDO_TABLESPACE切换到新建的U ...

  4. wannafly 练习赛10 f 序列查询(莫队,分块预处理,链表存已有次数)

    链接:https://www.nowcoder.net/acm/contest/58/F 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 262144K,其他语言524288K 64 ...

  5. POST上传多张图片配合Django接受多张图片

    POST上传多张图片配合Django接受多张图片 本地:POST发送文件,使用的是files参数,将本地的图片以二进制的方式发送给服务器. 在这里 files=[("img",op ...

  6. Linux NTP服务器的搭建及client自动更新时间

    Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间 ...

  7. .net sqlite 内存溢出 问题的分析与解决。

    一个小的工具网站,用了sqlite数据库,在并发小的情况一切正常,但是并发量上来之后,就报"out of memory"错误了. 分析了代码,就是很简单的根据一个条件取一段数据,并 ...

  8. SSH port forwarding: bind: Cannot assign requested address

    https://www.electricmonk.nl/log/2014/09/24/ssh-port-forwarding-bind-cannot-assign-requested-address/

  9. hook C++

    Intercepting Calls to COM Interfaces(hook com接口) 通过COM组件IFileOperation越权复制文件 代码注入之远程线程篇 使用VC++通过远程进程 ...

  10. ef和ashx的简单测试

    在这里.小弟我默默的计算了一下用一般处理程序和MVC配合EF,到底哪个快,事实问题是我感觉都差不多,因为用的是EF,所以要提高访问速率,我觉得还是要从数据访问做起: 一般处理程序代码: private ...