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] ...
随机推荐
- LeetCode - 不邻接植花
有 N 个花园,按从 1 到 N 标记.在每个花园中,你打算种下四种花之一. paths[i] = [x, y] 描述了花园 x 到花园 y 的双向路径. 另外,没有花园有 3 条以上的路径可以进入或 ...
- mven pom.xml Overriding managed version 问题解决详解
问题原因:在于默认的parent中的版本springboot有固定的指定 删除指定版本 <dependency> <groupId>junit</groupId> ...
- HOG行人目标检测
行人检测是自动驾驶中重要的内容,对于驾驶安全具有重要意义. HOG特征提取: (1)灰度化处理 (2)Gamma变换和梯度计算 (3)Cell划分 (4)Cell组成block,归一化处理 (5)bl ...
- Httpwatch抓包
一.下载Httpwatch 二.抓包 1.启动Httpwatch 打开浏览器-选择工具-Httpwatch professional(仅适用于IE和火狐40及以下浏览器) 2.开始抓包 点击“Reco ...
- SQL server 从创建数据库到查询数据的简单操作
目录. 创建数据库 创建表 插入数据 查询 1.创建数据库 --创建数据库 create database db_Product go --使用数据库use db_Productgo 2.创建表 -- ...
- python正则之match search findall
match:只匹配一次,开头匹配不上,则不继续匹配 a,b,\w+ match(a,"abcdef") 匹配a >>> re.match("a" ...
- iterator删除元素
总结 在需要的删除等操作时,不能使用简单的foreach,因为其底层依然用的是Iterator,但是调用的是集合中的remove方法. 使用迭代器对象调用其中的remove方法,以保证线程同步.
- POJO / Javabean / Entity Bean
POJO 和JavaBean是我们常见的两个关键字,一般容易混淆,POJO全称是Plain Ordinary Java Object / Pure Old Java Object,中文可以翻译成:普通 ...
- Pyhton实用的format()格式化函数
Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前的 % . format 函数可以接受不限个参数 ...
- Python 使用 PyQt5 开发的关机小工具
前两天简单认识了一下PyQt5,通过练习开发了一款在Window下自定义关机的小工具,代码如下 import os,sys,time from PyQt5 import QtCore,QtWidget ...