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. Linux下利用expect,不用交互模式,直接登陆远程主机

    Linux环境下只有在机器20.200.254.18上ssh dataconv@20.200.31.23才能连接到23的机器,而且还需要输入密码(每次都需要输入地址,密码很烦),所以利用expect写 ...

  2. unable to dequeue a cell with identifier MealTableViewCell

    1 问题描述 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable ...

  3. 认识:ThinkPHP的编译缓存文件~runtime.php

    1.定义单入口文件(index.php) 在单入口index.php中不定义这两项时,会生成编译缓存文件~runtime.php define('RUNTIME_PATH','./App/Temp/' ...

  4. Spring 具名参数NamedParameterJdbcTemplate

    具名参数: 具名参数:SQL 按名称(以冒号开头)而不是按位置进行指定. 具名参数更易于维护, 也提升了可读性. 具名参数由框架类在运行时用占位符取代 我们之前一直是用JDBCTemplate  进行 ...

  5. 第5章 不要让线程成为脱缰的野马(Keeping your Threads on Leash) ---线程优先权(Thread priority)

    有没有过这样的经验?你坐在你的车子里,目的地还在好几公里之遥,而时间已经很晚了.你拼命想告诉那些挡住你去路的人们,今天这个约会对你是多么多么重要,能不能请他们统统--呃--滚到马路外?很不幸,道路系统 ...

  6. 兼容低版本JS的Array.map方法

    前几天去别的公司面试遇到个这样的问题,兼容IE7下的Array.map方法,一脸蒙蔽.后面回来查了下资料发现.Array.map方法是ECMA-262 标准中新添加的方法,在低版本的JS中是木有的. ...

  7. 屏蔽掉Google Chrome 浏览器 textarea 单词拼写检测

    可以使用html5的spellcheck属性来关闭对元素内容进行拼写检查. <!-以下两种书写方法正确--> <textarea spellcheck="true" ...

  8. 550 Create directory operation failed

    往Linux系统中上传文件时候,我们经常会使用FTP连接Linux,也经常会使用mkdir命令来创建目录.最近发现用mkdir创建目录时提示550 Create directory operation ...

  9. 神奇的版本库—————GIT

    表示是第一次接触这个东东,然后疯狂百度了一波资料,然而=-=,完全不敢相信居然百度出了,GIT是全球最大同性交友网站...... 简直有点毁三观呐..好吧,其实按道理来说,这么解释也没有错欸,官方说明 ...

  10. Html事件冒泡

    原以为span不同于input,事件冒泡会被父级标签吞噬,写了个测试事件冒泡的Demo,发现并不是想得那样.另外:event.stopPropagation()以及event.stopImmediat ...