#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 = 110;
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=1;i<=n;i++)scanf("%d",&s[i]), sum[i]=sum[i-1]+s[i], dp[i][i][1]=0;;for(int len=2; len<=n; len++){for(int i=1; i+len-1<=n; i++) {int j = i+len-1;for(int k=2;k<=min(len,r);k++) {for(int c=i+k-2; c<j ;c++) {dp[i][j][k] = min(dp[i][j][k],dp[i][c][k-1]+dp[c+1][j][1]);}}for(int k=l-1;k<=r-1;k++) {for(int c=i+k-1; c<j ;c++) {dp[i][j][1] = min(dp[i][j][1],dp[i][c][k] + dp[c+1][j][1] + sum[j]-sum[i-1]);}}}}printf("%d\n",dp[1][n][1]==INF?0:dp[1][n][1]);}return 0;
}

转载于:https://www.cnblogs.com/Draymonder/p/9554381.html

icpc 2017北京 J题 Pangu and Stones 区间DP相关推荐

  1. 2017 北京赛区 J题 Pangu and Stones 【区间DP】

    题目链接:https://vjudge.net/problem/HihoCoder-1636: 题意:n堆石子,每次可以合并连续的 [ L~R ]堆石子,求最少的代价: /* 思路:区间DP;dp[i ...

  2. 2017ICPC北京 J:Pangu and Stones(区间DP)

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

  3. C - Pangu and Stones 区间DP

    题面链接:https://cn.vjudge.net/contest/319950#problem/C 题意:将N个石头合并每一次可以合并[L,R]的石头,并且消费这些被合并石头的价格总和.问把N个石 ...

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

    LINK 定义f[i][j][k]f[i][j][k]f[i][j][k]表示[i,j][i,j][i,j]合并成了kkk堆的最小代价 枚举区间长度,区间左右端点两重循环,枚举kkk一重循环 考虑把拼 ...

  5. 2021 ICPC 沈阳赛区J题 Luggage Lock

    2021 ICPC 沈阳赛区J题 Luggage Lock 题意 有TTT组样例,其中每组样例为: 给定一个密码为b0b1b2b3b_0b_1b_2b_3b0​b1​b2​b3​的密码锁,已知当前密码 ...

  6. [ICPC 北京 2017 J题]HihoCoder 1636 Pangu and Stones

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

  7. Pangu and Stones HihoCoder - 1636 (区间DP) 2017区域赛北京站

    Pangu and Stones 题目链接:HihoCoder - 1636 题意:现有n堆石子,每次合并k堆石子,L<=k<=R,每次合并石子花费代价是合并的石子的总数量:问最后能否合并 ...

  8. Pangu and Stones(区间 dp)

    Pangu and Stones 状态转移方程 d[i][j][k] 代表 i 到 j 这段区间合并成 k 段的最小花费 k != 1 :dp[i][j][k] = min(dp[i][kk][[1] ...

  9. 牛客网——2017校招真题在线编程(pythonC++)

    牛客网--2017校招真题在线编程(python&C++) 1.n个数里的最小k个 题目描述 找出n个数里最小的k个 输入描述: 每个测试输入包含空格分割的n+1个整数,最后一个整数为k值,n ...

最新文章

  1. 130.被围绕的区域
  2. 【BETA】Mac技巧之查看苹果电脑 Mac OS X 系统是否开启 64 位运算,以及设置 32/64 位模式的方法...
  3. Linux学习之系统编程篇:使用条件变量实现“生产者和消费者模型”
  4. mysql accountlevel1_mysql---修改表结构
  5. Linq to Oracle 使用教程(八)使用 T4 模版生成代码
  6. 简单的反射 把datatable 转换成list对象
  7. .NET6之MiniAPI(十六):数据保护
  8. python怎么画图片 wafer map_Python wafer_map包_程序模块 - PyPI - Python中文网
  9. leetcode 208 python3
  10. 详细设计 存储分配_零基础学C语言(7):存储类型
  11. 对于计算机专业英语的问题,计算机专业英语的问题
  12. tensorRT程序设计框架_4
  13. SSM房屋租赁系统,房屋合租系统 租房系统 SpringBoot租房系统
  14. WordPress优化攻略:全面提升WP网站速度仅需3个加速方法和1个插件
  15. 【新东方的全套价值上亿英语资料】好不容易搞到的~
  16. 一些常用网站的总结与分享
  17. 两个ViewControllerScene互相show,全局变量失效?简直就是zuo!
  18. (九)学习笔记autoware源码core_planning(lane_select)
  19. python numpy.fft.fft和ifft
  20. Linux下at命令的使用!

热门文章

  1. 计算机图形学 实验三 相机、阴影、光照
  2. k8s中pv和pvc如何理解
  3. 使用C++和C语言输入输出16进制数、8进制数
  4. vue 中 canvas 和svg合用制作地图
  5. 船长的AppFuse2.0学习进程
  6. java springBoot实现QQ机器人,定时发送信息,自动回复功能
  7. 新增表单元素和表单属性
  8. 复习笔记--用户调研
  9. icon-font字体颜色
  10. 半路出家自学当程序员这一年的经历,大家一起共勉