codeforces 229/D 动态规划

原创 2015年07月08日 15:58:55

题目链接:http://codeforces.com/problemset/problem/229/D

D. Towers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
5
8 2 7 3 1
output
3
input
3
5 2 1
output
2

题意:

这个城市有n座塔,为了美观,需要后面的塔不能比前面的塔矮,所以需要你来整理,你的操作方式是可以把任意一个塔放到他相邻的一座塔上,组成一座新塔,

注意只能移到相邻的塔上,新塔还能继续移动,问你最少多少步可以完成

思路:

一开始我天真的以为是贪心,想在O(n)的复杂度上解决他,代码也写出来了,过了很多组数据,CF给后台,当我看那组我WA的数据的时候,我才意识到我的思路是完全错的,就扔掉了,过了好几个月才捡起来想了个DP的解法:

二维DP,dp[i][j]:i表示第i座塔移动的j步,他能达到的最矮的情况,dp不断更新,跑一遍O(n^2)能解决,

#include <bits/stdc++.h>
using namespace std;
int dp[5002][5002];//第i个塔,用了j步骤的高度,向前压,向后压
const int inf=0x7f7f7f7f;
class Solution
{
    int n,a[5002];
public:
    Solution(int _n):n(_n)
    {
        for(int i=0; i<n; ++i)
            scanf("%d",&a[i]);
        memset(dp,0x7f,sizeof(dp));//字节赋值
        dp[0][0]=a[0];
    }
    void win()
    {
        for(int i=1; i<n; ++i) //枚举塔
        for(int j=0; j<i; ++j) //枚举前点
        {
            if(dp[i-1][j] == inf) continue;
            if(a[i] < dp[i-1][j])
            {
                dp[i][j+1]=min(dp[i-1][j]+a[i],dp[i][j+1]);//先直接加到前面,后面再决策他是否最优
                int k=0,rp=a[i];//向后找
                while(rp<dp[i-1][j])
                    rp+=a[(++k) + i];
                if(rp<dp[i-1][j])//后面所以的值都加起来都没前面的高,这个情况应舍弃
                    continue;
                dp[i+k][j+k]=min(rp,dp[i+k][j+k]);
            }
            else dp[i][j]=a[i];
        }
        for(int i=0; i<=5000; ++i)
        if(dp[n-1][i]<inf)
        {
            printf("%d\n",i);
            break;
        }
    }
};
int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        Solution OMG(n);
        OMG.win();//OMG必胜,S5冠军,加油!
    }
    return 0;
}




版权声明:本文为 calamity_coming 原创文章,转载什么的,排版弄好看些。。。

Codeforces 448D(动态规划、类H数)

这道题可以用类似H数(Humble Number)的方法做。 用一个数组f[1..n]记录

CodeForces 273D|Dima and Figure|动态规划

N*M(≤150\leq 150)的棋盘有多少个连通块是凸的。绕了好几个弯之后终于搞定了。。 令f[i][j][k][l]f[i][j][k][l]表示现在到了第i行,当前连通块占用j~k列,状态为...

Codeforces Round #422 (Div. 2) D. My pretty girl Noora 动态规划

题目链接: My pretty girl Noora题目大意选美比赛, 有n个人参加, 分成若干轮, 每轮分成多个小组, 每个小组人数必须相等, 每个小组要选出一个最美的, 选法是两两比较, 所以x个...
  • litmxs
  • litmxs
  • 2017年07月03日 21:06
  • 184

Codeforces 809D [平衡树][动态规划]

DescriptionDescription大概是求类似于给定区间的最长上升子序列SolutionSolutiongjghfd大神教我用平衡树维护,其实每次加入一个区间只会对LISLIS的DPDP数组...

Codeforces 379D. New Year Letter【动态规划、暴力枚举】

题目大意: 给出如下定义: s(n)=s(n-2)+s(n-1),其中s为字符串,+为串联,比如s1="ab",s2="cd",s1+s2="abcd",s2+s1="cdab"。 给出k,x,n...

【动态规划】【数位DP】[Codeforces 55 D]Beautiful numbers

题目描述Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer ...

CodeForces 432D|Prefixes and Suffixes|KMP|动态规划

题目大意求s的不同长度的既是s前缀又是s后缀的子串的个数题解考虑KMP的next数组。 因为next[j]表示lcp(s[1..j],s[1..k])(k...

codeforces 771D Bear ans Company 动态规划

E. Bear and Company time limit per test 1 second memory limit per test 256 megabytes inpu...
  • jijijix
  • jijijix
  • 2017年03月28日 17:12
  • 424

【动态规划12】codeforces813D Two Melodies(官方题解翻译)

codeforces813D 诡异题解
  • Flanoc
  • Flanoc
  • 2017年06月09日 20:18
  • 171

Codeforces Round #340 (Div. 2) (629A,629B,629C(排列组合,动态规划),629D(线段树))

Far Relative’s Birthday Cake 题目链接: http://www.codeforces.com/problemset/problem/629/A 解题思路: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:codeforces 229/D 动态规划
举报原因:
原因补充:

(最多只允许输入30个字)