The city of D consists of n towers, built consecutively on a straight line. The height of the tower that goes i-th (from left to right) in the sequence equals hi. The city mayor decided to rebuild the city to make it beautiful. In a beautiful city all towers are are arranged in non-descending order of their height from left to right.

The rebuilding consists of performing several (perhaps zero) operations. An operation constitutes using a crane to take any tower and put it altogether on the top of some other neighboring tower. In other words, we can take the tower that stands i-th and put it on the top of either the (i - 1)-th tower (if it exists), or the (i + 1)-th tower (of it exists). The height of the resulting tower equals the sum of heights of the two towers that were put together. After that the two towers can't be split by any means, but more similar operations can be performed on the resulting tower. Note that after each operation the total number of towers on the straight line decreases by 1.

Help the mayor determine the minimum number of operations required to make the city beautiful.

Input

The first line contains a single integer n (1 ≤ n ≤ 5000) — the number of towers in the city. The next line contains n space-separated integers: the i-th number hi (1 ≤ hi ≤ 105) determines the height of the tower that is i-th (from left to right) in the initial tower sequence.

Output

Print a single integer — the minimum number of operations needed to make the city beautiful.

Example

Input
5
8 2 7 3 1
Output
3
Input
3
5 2 1
Output
2
题意: 给出n个正整数,进行若干个操作,使得序列非减,求最少的操作次数;
            操作:
                    每次可以选择两个相邻的数合并为一个;
 
解法:
         (1) dp[i][j]表示 前i个整数合并成非减序列的最小代价,且最后一段区间为j->i
         (2) 枚举最后一段合并的区间;

dp(i)表示使得前i个塔美丽的最小操作次数,sum(i)表示前i座塔的前缀和,last(i)表示使得前i个塔美丽操作次数最小的情况下,最右侧一座塔最小的塔高。

那么就有状态转移方程:dp(i)=min{dp(j)+i-j+1},sum(i)-sum(j)>=last(j).

#include <cstdio>
int dp[],sum[],last[];
int main()
{
int n;
scanf("%d",&n);
for(int i = ;i <= n;i++){
int a;
scanf("%d",&a);
sum[i] = sum[i-]+a;
dp[i] = last[i] = <<;
}
for(int i = ;i <= n;i++){
for(int j = ;j < i;j++){
if(sum[i]-sum[j] >= last[j] && dp[i] >= dp[j]+i-j-){
dp[i] = dp[j]+i-j-;
if(last[i] > sum[i]-sum[j]) last[i] = sum[i]-sum[j];
}
}
}
printf("%d\n",dp[n]);
return ;
}
原文地址http://blog.sina.com.cn/s/blog_140e100580102wkl5.html

Towers CodeForces - 229D的更多相关文章

  1. Codeforces 229D Towers

    http://codeforces.com/problemset/problem/229/D 题意:有n(1<=n<=5,000)座塔排在一条直线上,从左到右每个塔的高度分别为hi(1&l ...

  2. B - Alyona and towers CodeForces - 739C

    链接: https://vjudge.net/contest/202699#problem/B 题意: 给出一个序列,要支持区间加和操作 求其中最长的区间,该区间内的元素满足(ai<ai+1&l ...

  3. Alyona and towers CodeForces - 739C (线段树)

    大意: 给定序列, 要求实现区间加, 询问整个序列最长的先增后减的区间. 线段树维护左右两端递增,递减,先增后减的长度即可, 要注意严格递增, 合并时要注意相等的情况, 要注意相加会爆int. #in ...

  4. Codeforces 626C Block Towers(二分)

    C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...

  5. Codeforces Beta Round #37 A. Towers 水题

    A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...

  6. codeforces 479B Towers 解题报告

    题目链接:http://codeforces.com/problemset/problem/479/B 题目意思:有 n 座塔,第 i 座塔有 ai 个cubes在上面.规定每一次操作是从最多 cub ...

  7. Codeforces 478D Red-Green Towers

    http://codeforces.com/problemset/problem/478/D 思路:dp:f[i][j]代表当前第i层,用了j个绿色方块的方案数,用滚动数组,还有,数组清零的时候一定要 ...

  8. Codeforces Round #274 (Div. 2) B. Towers

    As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting o ...

  9. Codeforces 739C Alyona and towers 线段树

    Alyona and towers 这个题写起来真的要人命... 我们发现一个区间被加上一个d的时候, 内部的结构是不变的, 改变的只是左端点右端点的值, 这样就能区间合并了. 如果用差分的话会简单一 ...

随机推荐

  1. JVM菜鸟进阶高手之路十二(jdk9、JVM方面变化, 蹭热度)

    转载请注明原创出处,谢谢! 经过 4 次跳票,历经曲折的 Java 9 正式版终于发布了!今天看着到处都是jdk9发布了,新特性说明,心想这么好的蹭热度计划能错过嘛,哈哈,所以就发了这篇文章. 目前j ...

  2. [解读REST] 4.基于网络应用的架构风格

    上篇文章介绍了一组自洽的术语来描述和解释软件架构:如何利用架构属性评估一个架构风格:以及对于基于网络的应用架构来说,那些架构属性是值得我们重点关注评估的.本篇在以上的基础上,列举一下一些常见的(RES ...

  3. memcached readme

    memcache======== http://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html # 命令 ## 存 ...

  4. Spring Boot 学习之项目构建

    最近做了外包,都是工程专业术语,前期熟悉项目看文档看的挺累的,闲暇时间自己学习一下Spring Cloud,找点乐趣. 就有了下面的小项目. 本项目是一个Spring boot项目. 一.nginx做 ...

  5. 【个人笔记】《知了堂》express模块

    NPM  包管理器 Node package module  ==>简称npm 类似的bower 安装express 1.全局Npm install express -g 2.项目中安装 项目中 ...

  6. 在Storyboard中为UITableView添加Header和Footer

    我在这里所说的Header和Footer并不是sectionHeader和sectionFooter,而是指UITableView的tableHeaderView和tableFooterView,这两 ...

  7. 洗礼灵魂,修炼python(4)--从简单案列中揭示常用内置函数以及数据类型

    上一篇说到print语句,print是可以打印任何类型到屏幕上,都有哪些类型呢? 整形(int) 长整型(long) 浮点型(float) 字符型(str) 布尔型(bool) 最常见的就这几种. 在 ...

  8. HDU 5974 数学

    A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  9. 记一次Linux下JavaWeb环境的搭建

    今天重装了腾讯云VPS的系统,那么几乎所有运行环境都要重新部署了.过程不难懂,但是也比较繁琐,这次就写下来,方便他人也方便自己日后参考参考. 我采用的是JDK+Tomcat的形式来进行JavaWeb初 ...

  10. Python数据分析(二): Numpy技巧 (3/4)

    numpy.pandas.matplotlib(+seaborn)是python数据分析/机器学习的基本工具. numpy的内容特别丰富,我这里只能介绍一下比较常见的方法和属性.   昨天晚上发了第一 ...