zoukankan      html  css  js  c++  java
  • codeforces 256 div2 C. Painting Fence 分治

    C. Painting Fence
    time limit per test
    1 second
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    Bizon the Champion isn't just attentive, he also is very hardworking.

    Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of ai meters.

    Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.

    Input

    The first line contains integer n (1 ≤ n ≤ 5000) — the number of fence planks. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

    Output

    Print a single integer — the minimum number of strokes needed to paint the whole fence.

    Examples
    input
    5
    2 2 1 2 1
    output
    3
    input
    2
    2 2
    output
    2
    input
    1
    5
    output
    1
    题意:给你一个栅栏,长度不等,宽度为1,用一个宽为1的刷子刷,最少刷几次;
    思路:有点dp的思想,将一个大问题,分成多个小问题,每个问题的解决都一样,计算横着刷,与竖着刷的最小值;
    #include<bits/stdc++.h>
    using namespace std;
    #define ll __int64
    #define mod 1000000007
    #define pi (4*atan(1.0))
    const int N=1e2+10,M=1e5+10,inf=1e9+10;
    int a[M],ans;
    int dfs(int l,int r)
    {
        int minn=inf;
        int sum=0;
        int shu=(r-l+1);
        for(int i=l;i<=r;i++)
        minn=min(a[i],minn);
        for(int i=l;i<=r;i++)
        a[i]-=minn;
        sum+=minn;
        int st=l;
        for(int i=l;i<=r;i++)
        if(a[i]==0)
        {
            sum+=dfs(st,i-1);
            st=i+1;
        }
        if(st<=r)
        sum+=dfs(st,r);
        return min(sum,shu);
    }
    int main()
    {
        int x,y,z,i,t;
        scanf("%d",&x);
        for(i=1;i<=x;i++)
        scanf("%d",&a[i]);
        cout<<dfs(1,x)<<endl;
        return 0;
    }
  • 相关阅读:
    MySql中启用InnoDB数据引擎的方法
    云说的到底对不对,京东到底行不行?
    hibernate HQL查询的参数绑定
    MySQL到底能支持多大的数据量?
    C# RSA和Java RSA互通
    Log4j 2使用教程
    Log4j.properties配置详解
    JMX 基础Demo
    iBatis缓存实现源码分析-FIFO,LUR实现方法
    SqlMapClient 创建过程之SqlMapConfigParser源码走读
  • 原文地址:https://www.cnblogs.com/jhz033/p/5571233.html
Copyright © 2011-2022 走看看