hihocoder 1636 : Pangu and Stones(区间dp)
Pangu and Stones
描述
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)的更多相关文章
- 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 ...
- [ICPC 北京 2017 J题]HihoCoder 1636 Pangu and Stones
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- icpc 2017北京 J题 Pangu and Stones 区间DP
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- 2017北京网络赛 J Pangu and Stones 区间DP(石子归并)
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- HihoCoder - 1636 Pangu and Stones(区间DP)
有n堆石子,每次你可以把相邻的最少L堆,最多R堆合并成一堆. 问把所有石子合并成一堆石子的最少花费是多少. 如果不能合并,输出0. 石子合并的变种问题. 用dp[l][r][k]表示将 l 到 r 之 ...
- HihoCoder 1636 Pangu and Stones(区间DP)题解
题意:合并石子,每次只能合并l~r堆成1堆,代价是新石堆石子个数,问最后能不能合成1堆,不能输出0,能输出最小代价 思路:dp[l][r][t]表示把l到r的石堆合并成t需要的最小代价. 当t == ...
- 2017ICPC北京 J:Pangu and Stones
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- Pangu and Stones HihoCoder - 1636 区间DP
Pangu and Stones HihoCoder - 1636 题意 给你\(n\)堆石子,每次只能合成\(x\)堆石子\((x\in[L, R])\),问把所有石子合成一堆的最小花费. 思路 和 ...
- Pangu and Stones(HihoCoder-1636)(17北京OL)【区间DP】
题意:有n堆石头,盘古每次可以选择连续的x堆合并,所需时间为x堆石头的数量之和,x∈[l,r],现在要求,能否将石头合并成一堆,如果能,最短时间是多少. 思路:(参考了ACM算法日常)DP[i][j] ...
随机推荐
- java中System类
System作为系统类,在JDK的java.lang包中,可见它也是一种java的核心语言特性.System类的构造器由private修饰,不允许被实例化.因此,类中的方法也都是static修饰的静态 ...
- BZOJ 1069 Luogu P4166 最大土地面积 (凸包)
题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=1069 (luogu)https://www.luogu.org/probl ...
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'ambari'
配置Ambari远程maridb 报错: ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'ambari' ...
- 用命令行编译运行java文件的乱码问题
之前在写的时候没有遇到过这个问题,用惯了eclipse之后突然用Notepad++就出现乱码了 我在编写的时候 指定Noepad++的编码是 UTF-8编码,然后进入命令行,编译的时候就出现了乱码 然 ...
- ES6注
1.Promise构造函数 //resolve(成功),reject(失败)两个参数 function runAsync(){ var p = new Promise(function(resolve ...
- lua源码学习篇三:赋值表达式解析的流程
上节说到表达式的解析问题,exprstate函数用于解析普通的赋值表达式.lua语言支持多变量赋值.本文先从单变量赋值表达式讲起. a = b = c = a + b 对于简单的两个数的求和过程,lu ...
- 分布式任务队列 Celery
目录 目录 前言 简介 Celery 的应用场景 架构组成 Celery 应用基础 前言 分布式任务队列 Celery,Python 开发者必备技能,结合之前的 RabbitMQ 系列,深入梳理一下 ...
- Redis存储对象序列化和反序列化
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInpu ...
- 数据科学家人才危机现象,是FOMO还是Silver?
数据科学家人才危机现象,是FOMO还是Silver? 数据科学家的人才短缺和薪水高涨已经达到了顶板,未来还会持续下去吗? 在过去几年中,高级分析(#大数据#分析)空间一直经历着严重的FOMO(害怕错过 ...
- Linux 下创建静态库和动态库
1.创建静态链接库 2.创建动态链接库