时间限制: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堆的最小代价,如果无法做到输出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(石子归并)的更多相关文章

  1. 2015北京网络赛 J Scores bitset+分块

    2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...

  2. hihocoder1236(北京网络赛J):scores 分块+bitset

    北京网络赛的题- -.当时没思路,听大神们说是分块+bitset,想了一下发现确实可做,就试了一下,T了好多次终于过了 题意: 初始有n个人,每个人有五种能力值,现在有q个查询,每次查询给五个数代表查 ...

  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 题

    题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...

  5. hihocoder 1636 : Pangu and Stones(区间dp)

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

  6. hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题

    题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...

  7. 2015北京网络赛 J Clarke and puzzle 求五维偏序 分块+bitset

    Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc20 ...

  8. 2017北京网络赛 F Secret Poems 蛇形回路输出

    #1632 : Secret Poems 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 The Yongzheng Emperor (13 December 1678 – ...

  9. 2017 北京网络赛 E Cats and Fish

    Cats and Fish 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are many homeless cats in PKU campus. They ...

随机推荐

  1. 【PAT甲级】1068 Find More Coins (30 分)(背包/DP)

    题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. AAAAAccepted code: #define HAVE_ST ...

  2. Linux kali安装或更新之后出现乱码

    打开终端,输入以下命令,之后重启. apt-get install ttf-wqy-zenhei

  3. Nginx笔试题!

    1.Nginx实现HTTP及TCP负载均衡的模块?HTTP就是工作在七层协议TCP工作在四层协议 Nginx七层负载:七层通过虚拟的URL或主机名接收请求在server里面配置location反向代理 ...

  4. MySQL 远程连接问题 (Linux Server)

    Mysql Workbench 连接Ubuntu上的Mysql时报如下错误: 原因:查看  /etc/mysql/mysql.conf.d/mysqld.cnf # # Instead of skip ...

  5. php 基础知识 常见面试题

    1.echo.print_r.print.var_dump之间的区别 * echo.print是php语句,var_dump和print_r是函数 * echo 输出一个或多个字符串,中间以逗号隔开, ...

  6. Tensorflow机器学习入门——ModuleNotFoundError: No module named 'tensorflow.keras'

    这个bug的解决办法: # from tensorflow.keras import datasets, layers, models from tensorflow.python.keras imp ...

  7. SQL的四种连接(内连接,外连接)

    一,内连接(inner join) 内连接(INNER JOIN):分显式的和隐式的,返回连接表中符合连接条件和查询条件的数据行.(所谓的连接表就是数据库在做查询形成的中间表). 1.隐式的内连接 没 ...

  8. Eclipse开发快捷键

    ctrl+alt+r:查找资源 ctrl+o:快速outLine ctrl+e:快速切换编辑器 ctrl+./ctrl+1:下一个错误修改

  9. ➡️➡️➡️leetcode 需要每天打卡,养成习惯

    目录 待完成的 完成的 0204 0203 以前 java 的 ! 的操作 不像 c 那样自由,!不要使用在int 变量上 c ^ 是异或操作 体会:c中,malloc 后的新建的数组,默认不是0(j ...

  10. Linux系统的发展历史和学习前景介绍

    2020年了,我想来跟大家聊聊Linux运维这一行业,从几个方面说下行业的现状.如何学好Linux和如何成为专业运维人员以及云服务对于Linux运维的影响. 一.linux行业状况 我们都知道从199 ...