#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. SIGGRAPH 2017:深度学习与计算机图形学的碰撞

    每年由美国计算机协会(Association of Computing Machinery,简称ACM)计算机图形专业组举办的年会SIGGRAPH,是全球最负盛名的图形学和交互技术盛会.今年已经是这场 ...

  2. javaSe-反射3

    package com.java.chap07.sec04; public class Student { private String name; private Integer age; publ ...

  3. Java 文件操作-RandomAccessFile

    1. RandomAccessFile     Java提供了一个可以对文件随机访问的操作,访问包括读和写操作.该类名为RandomAccessFile.该类的读写是基于指针的操作. 1)文件访问模式 ...

  4. [学习笔记] C++ 历年试题解析(一)--判断题

    少说话.. 程序题链接:https://www.cnblogs.com/aoru45/p/9898691.html 14级试题---选择题 1. 引用在声明时必须对其初始化,以绑定某个已经存在的变量( ...

  5. Fruit Ninja(取随机数)

    链接:https://www.nowcoder.com/acm/contest/163/A来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 262144K,其他语言524 ...

  6. 原型模式 -- JavaScript语言的灵魂

    原型模式就是将原型对象指向创建对象的类,使这些类共享原型对象的方法与属性.JS是基于原型链实现对象之间的继承,是对属性或者方法的共享,而不是对属性和方法的复制. // 图片轮播类 var LoopIm ...

  7. SC || Chapter 8

    栈:方法调用和局部变量的存储位置,保存基本类型 堆:在一块内存里分为多个小块,每块包含 一个对象,或者未被占用

  8. idea spring boot启动项目上面有红色叉

    一打开IDEA,在启动debug项目有一个红色叉如下图 因为打开项目可以主项目的包没有加载进来,解决办法就是右击项目->maven->Reimport  就搞定了..

  9. vue 配置多页面应用

    前言: 本文基于vue 2.5.2, webpack 3.6.0(配置多页面原理类似,实现方法各有千秋,可根据需要进行定制化) vue 是单页面应用.但是在做大型项目时,单页面往往无法满足我们的需求, ...

  10. Library setup