#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,当时我想的做法都是TLE的
我这个好像不太好,多了一层复杂度
#include<bits/stdc++.h>
using namespace std;
const int N=;
int a[N],dp[N][N][N],n,l,r;
int main()
{
while(~scanf("%d%d%d",&n,&l,&r))
{
memset(dp,-,sizeof dp);
for(int i=; i<=n; i++)
{
scanf("%d",a+i);
dp[i][i][]=;
a[i]+=a[i-];
}
for(int z=; z<=n; z++)
for(int i=; i<=n; i++)
{
int j=i+z-;
for(int k=; k<=r; k++)
for(int t=i; t<j; t++)
{
if(dp[i][t][k-]==-||dp[t+][j][]==-)continue;
int f=dp[i][t][k-]+dp[t+][j][];
if(dp[i][j][k]==-||dp[i][j][k]>f)dp[i][j][k]=f;
if(k>=l&&k<=r&&(dp[i][j][]==-||dp[i][j][]>dp[i][j][k]+a[j]-a[i-]))
dp[i][j][]=dp[i][j][k]+a[j]-a[i-];
}
}
if(dp[][n][]==-)printf("%d\n",);
else printf("%d\n",dp[][n][]);
}
return ;
}

2017ICPC北京 J:Pangu and Stones的更多相关文章

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

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

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

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

  3. icpc 2017北京 J题 Pangu and Stones 区间DP

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

  4. hihoCoder 1636 Pangu and Stones

    hihoCoder 1636 Pangu and Stones 思路:区间dp. 状态:dp[i][j][k]表示i到j区间合并成k堆石子所需的最小花费. 初始状态:dp[i][j][j-i+1]=0 ...

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

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

  6. Pangu and Stones HihoCoder - 1636 区间DP

    Pangu and Stones HihoCoder - 1636 题意 给你\(n\)堆石子,每次只能合成\(x\)堆石子\((x\in[L, R])\),问把所有石子合成一堆的最小花费. 思路 和 ...

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

  8. Pangu and Stones(HihoCoder-1636)(17北京OL)【区间DP】

    题意:有n堆石头,盘古每次可以选择连续的x堆合并,所需时间为x堆石头的数量之和,x∈[l,r],现在要求,能否将石头合并成一堆,如果能,最短时间是多少. 思路:(参考了ACM算法日常)DP[i][j] ...

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

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

随机推荐

  1. Nginx 基本配置介绍

    一.什么是Nginx Nginx 是一个免费的,开源的,高性能的HTTP服务器和反向代理,以及IMAP / POP3代理服务器. Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资源消耗而闻 ...

  2. JAVA-Web04

    1 理解dom解析器机制   1)dom解析和dom4j原理一致   2)Node是所有元素的父接口   3)常用的API: DocumentBuilderFactory factory = Docu ...

  3. HDU 5489 Removed Interval (LIS,变形)

    题意: 给出一个n个元素的序列,要求从中删除任一段长度为L的连续子序列,问删除后的LIS是多少?(n<=10w, L<=n ,元素可能为负) 思路: 如果会O(nlogn)求普通LIS的算 ...

  4. 洛谷 P1074 靶形数独

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教, Z 博士拿出了他最近发明的 ...

  5. duboo 配置文件

    官方文档 http://dubbo.apache.org/en-us/docs/user/quick-start.html 自己的 <?xml version="1.0" e ...

  6. Cross-Entropy Loss 与Accuracy的数值关系(很重要,很好的博客)

    http://www.cnblogs.com/dengdan890730/p/6132937.html

  7. javaweb基础(18)_jsp属性范围

    所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围. 一.JSP属性范围 JSP中提供了四种属性范围,四种属性范围分别指以下四种: 当前页:一个属性只能在一个页面中取得 ...

  8. Bootstrap历练实例:面板的标题

    面板标题 我们可以通过以下两种方式来添加面板标题: 使用 .panel-heading class 可以很简单地向面板添加标题容器.to easily add a heading container ...

  9. Java 练习:字符串反转

    package com.swift; public class String_Reverse_Test { public static void main(String[] args) { /* * ...

  10. JS数据结构与算法--双向链表

    双向链表中链接是双向的:一个链向下一个元素,另一个链向上一个元素,如下图所示: 双向链表结构代码如下: class Node { constructor(element) { this.element ...