【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 ...
随机推荐
- 【vue】混合模式
因为工作的分配,写财务的对账部分,因为3个页面的设计和功能基本相同,都是查询筛选表格,所以用混合模式优化了部分代码.用混合把一些共用的东西抽离了出来. 具体使用方法参照文档. https://cn.v ...
- (C# SQL Server) 数据库连接字符串
Debug 一个SQL Server 2008 数据库连接的问题,老是提示: [67944] System.Data.SqlClient.SqlException (0x80131904): 在与 S ...
- error C3861: “getpid”: 找不到标识符
原文:http://blog.csdn.net/woniu199166/article/details/52471242 这种错误一般就是没有对应的函数或者对应的头文件 旧版的vs添加#include ...
- [C++]多线程: 教你写第一个线程
原文:http://blog.csdn.net/cn_wk/article/details/62236057 hello thread! 声明线程A的端口号 #include <pthread. ...
- Android 黑色样式menu
效果图:
- python迭代器 生成器 三元运算 列表解析
1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大优 ...
- Azure 订阅和服务限制、配额和约束
最后更新时间:2016年10月24日 概述 本文档指定一些最常见的 Azure 限制.请注意,本文档目前未涵盖所有 Azure 服务.一段时间后,将展开并更新这些限制以包含多个平台. NOTE: 如果 ...
- C#/Entity Frame Core 使用Linq 进行分页 .Skip() .Take() 的使用方法
一般使用格式为 int pagesize = 分页大小(每一页大小)int pageindex = 第几页(一般这个变量是随循环递增的) 使用方法.Skip(pagesize*pageindex).T ...
- MapReduce Design Patterns(chapter 1)(一)
Chapter 1.Design Patterns and MapReduce MapReduce 是一种运行于成百上千台机器上的处理数据的框架,目前被google,Hadoop等多家公司或社区广泛使 ...
- 提升PHP速度
PHP的优点之一是速度很快,对于一般的网站应用,可以说是已经足够了.不过如果站点的访问量很高.带宽窄或者其它的因素令服务器产生性能瓶颈的时候,你可能得想想其它的办法来进一步提高PHP的速度了.这篇文章 ...