2017北京网络赛 J Pangu and Stones 区间DP(石子归并)
描述
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堆的最小代价,如果无法做到输出0
题解:
石子归并系列题目,一般都是区间DP,于是——
dp[i][j][k] i到j 分为k堆的最小代价。显然 dp[i][j][ j-i+1]代价为0
然后[i,j] 可以划分
dp[i][j][k] = min { dp[i][d][k-1] + dp[d+1][j][1] } (k > 1&&d-i+1 >= k-1,这个条件意思就是 区间i,d之间最少要有k-1个石子)
最后合并的时候
dp[i][j][1] = min{ dp[i][d][k-1] + dp[d+1][j][1] + sum[j] - sum[i-1] } (l<=k<=r)
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<utility>
#include<map>
#include<queue>
#include<set>
#define mx 0x3f3f3f3f
#define ll long long
using namespace std;
int n, l,r;
int dp[][][],sum[],a[];
//dp[i][j][t]表示区间[i,j]分为k堆的最小代价
int main()
{
while (~scanf("%d%d%d",&n,&l,&r))
{
memset(dp,0x3f,sizeof(dp));//初始化为无穷大
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];//前缀和
}
for(int i=;i<=n;i++)//初始化dp
for(int j=i;j<=n;j++)
dp[i][j][j-i+]=;//[i,j]分成j-i+1堆的代价是0
for(int len=;len<=n;len++)
{
for(int i=;i<=n-len+;i++)
{
int j=i+len-;
for(int k=i;k<j;k++)//枚举分界点
{
for(int t=l;t<=r;t++)//堆数
dp[i][j][]=min(dp[i][j][],dp[i][k][t-]+dp[k+][j][]+sum[j]-sum[i-]);
for(int t=;t<j-i+;t++)
dp[i][j][t]=min(dp[i][j][t],dp[i][k][t-]+dp[k+][j][]);
} }
}
int ans=dp[][n][];
if(ans>=mx)
printf("0\n");
else
printf("%d\n",ans); }
}
2017北京网络赛 J Pangu and Stones 区间DP(石子归并)的更多相关文章
- 2015北京网络赛 J Scores bitset+分块
2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...
- hihocoder1236(北京网络赛J):scores 分块+bitset
北京网络赛的题- -.当时没思路,听大神们说是分块+bitset,想了一下发现确实可做,就试了一下,T了好多次终于过了 题意: 初始有n个人,每个人有五种能力值,现在有q个查询,每次查询给五个数代表查 ...
- icpc 2017北京 J题 Pangu and Stones 区间DP
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- 2017乌鲁木齐网络赛 j 题
题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...
- hihocoder 1636 : Pangu and Stones(区间dp)
Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the first livi ...
- hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题
题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...
- 2015北京网络赛 J Clarke and puzzle 求五维偏序 分块+bitset
Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc20 ...
- 2017北京网络赛 F Secret Poems 蛇形回路输出
#1632 : Secret Poems 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 The Yongzheng Emperor (13 December 1678 – ...
- 2017 北京网络赛 E Cats and Fish
Cats and Fish 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are many homeless cats in PKU campus. They ...
随机推荐
- 零基础入门深度学习(6) - 长短时记忆网络(LSTM)
代码: def forward(self, x): ''' 根据式1-式6进行前向计算 ''' self.times += 1 # 遗忘门 fg = self.calc_gate(x, self.Wf ...
- zigbee学习基础
应用场合:功耗(休眠)以及自组网(动态路由,梯度法寻径)是其特点.距离短.功耗低且传输速率不高的各种电子设备之间进行有周期性数据.间歇性数据和低反应时间数据传输的应用(智能家居/仓储中转/伞兵落地协同 ...
- vue项目注意事项
vue项目注意事项 1. 文件和路由命名规范 views里面代表的是你下面导航中的每一块,每个文件名 需要大写,路由命名全部小写,第一层路由就是最下面的那几个导航的名字,二级路由是在一 级路由的基础上 ...
- Linux下安装 boost 库
1. 先去官网下载压缩包: https://www.boost.org/ 2. 解压 tar -zvxf boost_1_70_0.tar.gz 2. cd 进入根目录,然后执行: ./bootstr ...
- Newtonsoft.Json 版本不一致导致错误
可以在配置文件添加这部分,其他版本的不一致,也可使用这种方式解决. <runtime> <assemblyBinding xmlns="urn:schemas-micros ...
- problem :无法显示activemq的管理界面
点击 Manage ActiveMQ broker 无法显示admin界面 解决方法:修改activemq.xml 和 jetty.xml文件 把所有0.0.0.0修改为127.0.0.1 成功: 账 ...
- 类似discuz密码的生成规则
/* 生成一个串,uniqid(rand()): uniqid(prefix,more_entropy) 函数基于以微秒计的当前时间,生成一个唯一的 ID. 如果 prefix 参数为空,则返回的字符 ...
- leetcode刷题-- 2. 排序(待更新)
排序 参考五分钟学算法 复杂度比较 时间复杂度 O(n2) 各种简单的排序:直接插入.直接选择.冒泡 O(nlog2n) 快速排序.堆排序.归并排序 O(n1+\(\lambda\)),希尔排序 线性 ...
- js里用 toLocaleString 实现给数字加三位一逗号间隔(有无小数点都适用)
<input type="hidden" id="totalLandArea" value="<%-info.totalLandArea% ...
- 解决:Field xxMapper in xx.service.impl.xxServiceImpl required a bean of type 'xx.mapper.xxMapper'
1.启动 SpringBoot项目报错,使用的是Springboot.Spring.Mybatis连接Mysql数据库,启动SpringBoot项目报错,错误如下所示: _____ .__/\ .__ ...