Towers codeforce +dp
来源:互联网 发布:智慧芽数据库 编辑:程序博客网 时间:2024/06/07 00:03
The city of D consists of n towers, built consecutively on a straight line. The height of the tower that goesi-th (from left to right) in the sequence equalshi. The city mayor decided to rebuild the city to make itbeautiful. 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 standsi-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 containsn 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.
Sample Input
58 2 7 3 1
3
35 2 1
2
解决方案:可推得递归方程:dp[i]=max(dp[j]+i-j-1,dp[i])&&last[j]<=sum[i]-sum[j];dp[i]代表前i个形成递增所需的最小步数,last[i]表示前i个的最高的塔的高度,sum[i],表示前i个塔总高度,每次选最优,并更新last[i],dp[i]。
code:#include<iostream>#include<cstdio>#include<cstring>using namespace std;int last[5050],dp[5050],sum[5050];int N;int main(){ scanf("%d",&N); for(int i=1;i<=N;i++){ int a; scanf("%d",&a); sum[i]+=sum[i-1]+a; dp[i]=1<<30; last[i]=1<<30; } for(int i=1;i<=N;i++) for(int j=0;j<i;j++){ if(sum[i]-sum[j]>=last[j]&&dp[i]>=dp[j]+i-j-1){ dp[i]=dp[j]+i-j-1; if(last[i]>sum[i]-sum[j]) last[i]=sum[i]-sum[j]; } } printf("%d\n",dp[N]); return 0;}
- Towers codeforce +dp
- codeforce 之 Red-Green Towers
- 229D - Towers DP
- ZOJ2059 The Twin Towers,DP
- UVA 1414 - Hanoi Towers(dp)
- uva 1414 - Hanoi Towers(dp)
- Hanoi Towers - POJ 3572 dp
- codeforces 229D Towers DP
- zoj2059 The Twin Towers (dp)
- D - Red-Green Towers dp
- codeforce 143D dp
- Codeforce 792C(dp)
- Towers
- Towers
- ZOJ2059The Twin Towers题解动态规划DP
- POJ 1958 Strange Towers of Hanoi [dp]
- Codeforces 229D Towers 贪心 + dp
- uva10066 - The Twin Towers(dp,lcs)
- 解决安装win7后ubuntu无法启动问题
- hdu 1875 畅通工程再续
- http://tgyd2006.iteye.com/blog/871048
- 4.将两个排好序的数组,合并到另外一个数组中,并且合并之后的数组也是有序的。
- Leetcode之四Remove Duplicates from Sorted Array II
- Towers codeforce +dp
- 北斗卫星导航系统BCH解码的实现结构
- 7.,一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同
- NS-3安装过程
- 简易播放器
- codeforces229D. Towers
- POJ3624 Charm Bracelet 【01背包】
- Linux系统中如何添加、使用硬盘
- View not attached to window manager解决方案