【2017 ICPC亚洲区域赛北京站 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'.
Input
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.
Output
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.
Sample Input
3 2 2
1 2 3
3 2 3
1 2 3
4 3 3
1 2 3 4
Sample Output
9
6
0
题意:
n个石子堆排成一排,每次可以将连续的[L,R]堆石子合并成一堆,花费为要合并的石子总数。求将所有石子合并成一堆的最小花费,如无法实现则输出0。
思路:
dp[i][j][k]表示将区间[i, j]合并成k堆的最小代价,转移有:
k=1时:
dp[i][j][1]=min(dp[i][j][1],dp[i][j][q]+sum[j]-sum[i-1])
k>1时:
dp[i][j][q]=min(dp[i][j][q],dp[i][k][q-1]+dp[k+1][j][1])
#include<bits/stdc++.h>
using namespace std;
#define MAX 105
#define INF 0x3f3f3f3f
int sum[MAX],dp[MAX][MAX][MAX];
int main()
{
int n,l,r,i,j,k;
while(scanf("%d%d%d",&n,&l,&r)!=EOF)
{
memset(dp,INF,sizeof(dp));
for(i=;i<=n;i++)
{
scanf("%d",&sum[i]);
dp[i][i][]=;
sum[i]+=sum[i-];
}
int len;
for(len=l;len<=r;len++) //merge长度 len[l,r]
{
for(i=;i+len-<=n;i++)//merge范围 [i,i+len-1]
{
j=i+len-;
dp[i][j][len]=;
dp[i][j][]=sum[j]-sum[i-];
}
} int q;
for(len=;len<=n;len++) //merge长度 len[2,n]
{
for(i=;i+len-<=n;i++)//merge范围 [i,i+len-1]
{
j=i+len-;
for(k=i;k<j;k++)
for(q=;q<=len;q++)
dp[i][j][q]=min(dp[i][j][q],dp[i][k][q-]+dp[k+][j][]);
for(q=l;q<=r;q++)
dp[i][j][]=min(dp[i][j][],dp[i][j][q]+sum[j]-sum[i-]);
}
} if(dp[][n][]<INF)
printf("%d\n",dp[][n][]);
else printf("0\n");
}
return ;
}
【2017 ICPC亚洲区域赛北京站 J】Pangu and Stones(区间dp)的更多相关文章
- 2017 ACM-ICPC亚洲区域赛北京站J题 Pangu and Stones 题解 区间DP
题目链接:http://www.hihocoder.com/problemset/problem/1636 题目描述 在中国古代神话中,盘古是时间第一个人并且开天辟地,它从混沌中醒来并把混沌分为天地. ...
- 2017北京网络赛 J Pangu and Stones 区间DP(石子归并)
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- 【2016 ICPC亚洲区域赛北京站 E】What a Ridiculous Election(BFS预处理)
Description In country Light Tower, a presidential election is going on. There are two candidates, ...
- 【2017 ICPC亚洲区域赛沈阳站 K】Rabbits(思维)
Problem Description Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number li ...
- 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)
队名:Unlimited Code Works(无尽编码) 队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...
- 2014ACM/ICPC亚洲区域赛牡丹江站汇总
球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...
- icpc 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)
Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the first livi ...
- 2017 ICPC西安区域赛 A - XOR (线段树并线性基)
链接:https://nanti.jisuanke.com/t/A1607 题面: Consider an array AA with n elements . Each of its eleme ...
随机推荐
- Java基础之StringBuffer和StringBuilder的区别
StringBuffer是一个字符串的缓存类,属于一个容器,对于容器,我们可以进行增删改查. StringBuffer的容器长度是可变的,并且里面可以存放多种的数据类型.它跟其他容器,比如数组,是很不 ...
- java线程面试手写题
1.设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1.写出程序. public class Question1 { private int j = 0; /** * @param ...
- spring boot(5)-properties参数配置
application.properties application.properties是spring boot默认的配置文件,spring boot默认会在以下两个路径搜索并加载这个文件 s ...
- hibernate简单入门教程(一)---------基本配置
应用级别所以很粗浅 首先介绍一下hibernate框架: 1.优秀的持久化(通俗讲把内存上的短时间运行信息存储在持久化硬盘上)框架. 2.作用于持久层,因为没什么侵入性,所以同样适用于其他层面上的存储 ...
- 沉淀,再出发:结合案例看python
沉淀,再出发:结合案例看python 一.前言 关于python,如果不经过大型程序开发的洗礼,我们很难说自己已经懂得了python了,因此,我们需要通过稍微结构化的编程来学习python. 二.一个 ...
- Linq使用技巧及查询示例(一)
Linq的使用大体分为两种:语句表达式 和 方法 首先,我们要在控制器中定义好context private ApplicationDbContext db = new ApplicationD ...
- 用timer自定义计划任务时间
应业务需求,需要将指定程序,按照指定时间进行运行, 而windows计划任务最小运行间隔时间为1分钟,完全不能满足当前需求, 有两种方案,一种是安装win服务方式,考滤到维护困难,另一种是timer方 ...
- ASP.NET 控件不可编辑
前台页面的解析后的html代码为disabled="disabled",而在后台我们需要设置控件的Enabled=false:即可.
- August 15th 2017 Week 33rd Tuesday
Would rather have done a regret, do not miss the regret. 宁愿做过了后悔,也不要错过了后悔. Yesterday, I read several ...
- JavaScript的事件的处理函数(鼠标,键盘,HTML)
事件处理函数概述 JavaScript 可以处理的事件类型为:鼠标事件.键盘事件.HTML 事件 所有的事件处理函数都会都有两个部分组成,on + 事件名称,例如 click 事件的事件处理函数就是: ...