icpc 2017北京 J题 Pangu and Stones 区间DP
#1636 : 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
dp[i][j][k] i到j 分为k堆的最小代价
显然 dp[i][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)
然后 需要初始化边界 dp[i][j][1] 当 i != j, dp[i][j][1] = 1
#include<bits/stdc++.h>
using namespace std; const int N = ;
const int INF = 0x3f3f3f3f;
int dp[N][N][N], s[N], sum[N];
int n,l,r; int main () {
//freopen("in.txt","r",stdin);
while (~scanf("%d %d %d",&n,&l,&r)) {
memset(dp,0x3f,sizeof(dp));
for(int i=;i<=n;i++)
scanf("%d",&s[i]), sum[i]=sum[i-]+s[i], dp[i][i][]=;; for(int len=; len<=n; len++){
for(int i=; i+len-<=n; i++) {
int j = i+len-;
for(int k=;k<=min(len,r);k++) {
for(int c=i+k-; c<j ;c++) {
dp[i][j][k] = min(dp[i][j][k],
dp[i][c][k-]+dp[c+][j][]);
}
} for(int k=l-;k<=r-;k++) {
for(int c=i+k-; c<j ;c++) {
dp[i][j][] = min(dp[i][j][],
dp[i][c][k] + dp[c+][j][] + sum[j]-sum[i-]);
}
}
}
}
printf("%d\n",dp[][n][]==INF?:dp[][n][]);
}
return ;
}
icpc 2017北京 J题 Pangu and Stones 区间DP的更多相关文章
- 2017北京网络赛 J Pangu and Stones 区间DP(石子归并)
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- 2017 ACM-ICPC亚洲区域赛北京站J题 Pangu and Stones 题解 区间DP
题目链接:http://www.hihocoder.com/problemset/problem/1636 题目描述 在中国古代神话中,盘古是时间第一个人并且开天辟地,它从混沌中醒来并把混沌分为天地. ...
- 2017ICPC北京 J:Pangu and Stones
#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 ...
- 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp
QSC and Master Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 【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 w ...
- hdu 4462 第37届ACM/ICPC 杭州赛区 J题
题意:有一块n*n的田,田上有一些点可以放置稻草人,再给出一些稻草人,每个稻草人有其覆盖的距离ri,距离为曼哈顿距离,求要覆盖到所有的格子最少需要放置几个稻草人 由于稻草人数量很少,所以状态压缩枚举, ...
- 2014多校第一场J题 || HDU 4870 Rating(DP || 高斯消元)
题目链接 题意 :小女孩注册了两个比赛的帐号,初始分值都为0,每做一次比赛如果排名在前两百名,rating涨50,否则降100,告诉你她每次比赛在前两百名的概率p,如果她每次做题都用两个账号中分数低的 ...
- 一道另类的区间dp题 -- P3147 [USACO16OPEN]262144
https://www.luogu.org/problemnew/show/P3147 此题与上一题完全一样,唯一不一样的就是数据范围; 上一题是248,而这一题是262144; 普通的区间dp表示状 ...
随机推荐
- struts2 错误:Dispatcher initialization failed java.lang.RuntimeException
严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationTargetE ...
- 虚拟机中安装Ubuntu 16.04
1.安装vmware软件(虚拟机管理软件) https://blog.csdn.net/salmon_zhang/article/details/79468918 2.安装Ubuntu https:/ ...
- 居然上了模板使用排行榜第一 happy一下
这段时间在学习css和div,顺便把博客给整了一下,然后不小心就上了FFandIE模板使用排行榜第一,happy一下下.不知道这个算不算排名,还是随机刷新.感觉应该是按流量统计的,这段时间有几篇文章一 ...
- MYSQL中的BlackHole引擎
MYSQL中的BlackHole引擎 http://blog.csdn.net/ylspirit/article/details/7234021 http://blog.chinaunix.net/u ...
- Inception系列
从GoogLeNet的Inceptionv1开始,发展了众多inception,如inception v2.v3.v4与Inception-ResNet-V2. 故事还是要从inception v1开 ...
- 十天精通CSS3(4)
text-overflow 与 word-wrap text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法: 但是text-overflow只是用来说明文字溢出时 ...
- 四、Mosquitto 高级应用之用户配置
本文将讲解 Mosquitto 用户机制.如果还没有搭建 Mosquitto 服务的可以参考我的另外两篇文章<< 一.Mosquitto 介绍&安装>> << ...
- clothes
- c++实现web服务框架
lamada表达式 声明一个返回数组指针的函数 返回指针数组的函数形式如下所示: 括号必须存在 注意->后不能加() Lambda表达式
- pycharm跳到指定的行
ctrl+g 弹出一个框,输入要跳的行数 ctrl+home 光标移到第一行 ctrl+end 光标移到最后一行