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表示状 ...
随机推荐
- 代码这样写更优雅(Python版)
要写出 Pythonic(优雅的.地道的.整洁的)代码,还要平时多观察那些大牛代码,Github 上有很多非常优秀的源代码值得阅读,比如:requests.flask.tornado,笔者列举一些常见 ...
- 帝国cms调用缩略图和具体文章的方法
我们在用帝国cms建站的时候经常会在首页或者分类页等调用一些文章,如果文章带有展示图也把图片调用出来.帝国cms调用缩略图和具体文章怎么操作呢?我们用帝国cms的灵动标签[e:loop],只要记住常用 ...
- Hadoop DistributedCache分布式缓存的使用
做项目的时候遇到一个问题,在Mapper和Reducer方法中处理目标数据时,先要去检索和匹配一个已存在的标签库,再对所处理的字段打标签.因为标签库不是很大,没必要用HBase.我的实现方法是把标签库 ...
- 图结构练习——判断给定图是否存在合法拓扑序列(sdutoj)
#include<stdio.h>#include<string.h>int d[15],map[15][15],vis[15];int main(){ int i,j, ...
- cmd 笔记(随时补充)
被一篇破解WIFI的标题文骗到了,所以学习一下CMD的命令 1 查看已经连接的wifi和密码 netsh wlan show profiles 回车 netsh wlan show profiles ...
- MyBatis学习笔记(六)——调用存储过程
转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4270352.html 一.提出需求 查询得到男性或女性的数量, 如果传入的是0就女性否则是男性 二.准备数据 ...
- centos 安装sftp服务
打开命令终端窗口,按以下步骤操作. 0.查看openssh的版本 ssh -V 使用ssh -V 命令来查看openssh的版本,版本必须大于4.8p1,低于的这个版本需要升级. 1.创建sftp组 ...
- SpringBoot——定时任务+WebSocket(问题)
开发环境:win7 + idea2018 + jdk 1.8 + springboot 2.x 记一次出现问题,我在项目中先集成了websocket环境,并且测试通过,之后想要模拟实时推送的效果,虽然 ...
- stm32 学习参考(转)
源:stm32 学习参考 单片机裸机下写一个自己的shell调试器 LWIP_STM32_ENC28J60_NETCONN_TCP_SERVICER(5) LWIP_STM32_ENC ...
- mysql 触发器 trigger用法 two (稍微复杂的)
触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/befo ...