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] ...
随机推荐
- protocol buffer第一篇:语法介绍
先理解一下protocol buffer是什么东西. protocol buffer是google发明的一种数据序列化方案,和json是同种类型的玩意,它非常适合在rpc场景下使用.同json一样,p ...
- linux 内存
[转]Linux 查看内存(free buffer cache) 转自:http://elf8848.iteye.com/blog/1995638 Linux下如何查内存信息,如内存总量.已使用量.可 ...
- Qt类图
Qt对象命名与类图 QWidget=Windows get,即获得窗体,凡是我们能看到的界面都是从QWidget继承而来的. QDialog QAbstractButton 这是一个抽象类,不能直接使 ...
- RPN
训练: 特征图是51x39x256,对该图像的每点考虑9个窗口:三种候选面积(128,256,512) x 三种尺度(1:1,1:2,2:1).这些候选窗口称为anchors.如下图: 如果图片尺寸w ...
- Java源码阅读-Integer(基于jdk1.8)
public final class Integer extends Number implements Comparable<Integer> Integer 由final修饰了,所以该 ...
- Array Stack Implement using C
- 三十八、python中反射介绍
一.反射:根据字符串的形式去对象(某个模块)中去操作成员通过字符串的形式,导入模块通过字符串的形式,去模块中寻找指定的函数,并执行 1.__import__:用于字符串的形似执行导入模块 inp=in ...
- MySQL——执行计划
项目开发中,性能是我们比较关注的问题,特别是数据库的性能:作为一个开发,经常和SQL语句打交道,想要写出合格的SQL语句,我们需要了解SQL语句在数据库中是如何扫描表.如何使用索引的: MySQL提供 ...
- 原生js去除行内样式
概述 今天我用js给dom元素设置样式,碰到了一些问题,记下来供以后开发时参考,相信对其他人也有用. 心得 js加上class: $dom.classList.add('some-class'); j ...
- k8s集群上线web静态网站
环境准备 一台部署节点,一台master节点,还有两台节点node1,node2 完好的k8s集群环境 思路1: 在node1和node2节点上通过宿主机与容器之间目录映射和端口映射上线静态网站(或动 ...