#1636 : Pangu and Stones

时间限制: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

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的更多相关文章

  1. 2017北京网络赛 J Pangu and Stones 区间DP(石子归并)

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

  2. 2017 ACM-ICPC亚洲区域赛北京站J题 Pangu and Stones 题解 区间DP

    题目链接:http://www.hihocoder.com/problemset/problem/1636 题目描述 在中国古代神话中,盘古是时间第一个人并且开天辟地,它从混沌中醒来并把混沌分为天地. ...

  3. 2017ICPC北京 J:Pangu and Stones

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

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

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

  5. 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) ...

  6. 【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 ...

  7. hdu 4462 第37届ACM/ICPC 杭州赛区 J题

    题意:有一块n*n的田,田上有一些点可以放置稻草人,再给出一些稻草人,每个稻草人有其覆盖的距离ri,距离为曼哈顿距离,求要覆盖到所有的格子最少需要放置几个稻草人 由于稻草人数量很少,所以状态压缩枚举, ...

  8. 2014多校第一场J题 || HDU 4870 Rating(DP || 高斯消元)

    题目链接 题意 :小女孩注册了两个比赛的帐号,初始分值都为0,每做一次比赛如果排名在前两百名,rating涨50,否则降100,告诉你她每次比赛在前两百名的概率p,如果她每次做题都用两个账号中分数低的 ...

  9. 一道另类的区间dp题 -- P3147 [USACO16OPEN]262144

    https://www.luogu.org/problemnew/show/P3147 此题与上一题完全一样,唯一不一样的就是数据范围; 上一题是248,而这一题是262144; 普通的区间dp表示状 ...

随机推荐

  1. Shell初学(三)传参

    一. 脚本代码:test.sh echo "Shell 传递参数实例!"; echo "执行的文件名:$0"; echo "第一个参数为:$1&quo ...

  2. Windows操作系统上各种服务使用的端口号, 以及它们使用的协议的列表

    Windows操作系统上各种服务使用的端口号, 以及它们使用的协议的列表 列表如下 Port Protocol Network Service System Service System Servic ...

  3. transition使用

  4. [LeetCode] 437. Path Sum III_ Easy tag: DFS

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  5. [LeetCode] 476. Number Complement_Easy tag: Bit Manipulation

    这个题目思路就是比如101 的结果是010, 可以从111^101 来得到, 那么我们就需要知道刚好比101多一位的1000, 所以利用 while i <= num : i <<= ...

  6. iOS UI基础-4.0应用程序管理

    功能与界面 功能分析: 以九宫格的形式展示应用信息 点击下载按钮后,做出相应的操作 步骤分析: 加载应用信息 根据应用的个数创建对应的view 监听下载按钮点击 整个应用界面: 程序实现 思路 UI布 ...

  7. Amazon OA

    Remove Duplicates from unsorted array,它的错误在于9-10行k out of bound,改成下面这样就没问题了 public class removeDupli ...

  8. CSS中 Zoom属性

    CSS中 Zoom属性 其实Zoom属性是IE浏览器的专有属性,Firefox等浏览器不支撑.它可以设置或检索对象的缩放比例.除此之外,它还有其他一些小感化,比如触发ie的hasLayout属性,清除 ...

  9. 有意思的JSON.parse()、JSON.stringify()

    前言 现在JSON格式在web开发中非常重要,特别是在使用ajax开发项目的过程中,经常需要将后端响应的JSON格式的字符串返回到前端,前端解析成JS对象值(JSON 对象),再对页面进行渲染. 在数 ...

  10. sql 事务运用实例

    ------------------------------ create proc SaveFinancialProduct@FinancialName nvarchar(50),--产品名称@Yi ...