zoukankan      html  css  js  c++  java
  • SPOJ:House Fence(分治&DP)

    "Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his college. On this holiday, Joke plans to go to his grandmother's house located in Schematics village. Joke's grandmother's house is more than a hundred years old. Joke is very kind hearted, so he wants to help renovate the house by painting the fence. The fence consists of N vertical boards placed on a line. The boards are numbered 1 to N from left to right, and each of them has the length of 1 meter and the height of Ai meters. 

    Joke's grandmother has a paintbrush that can be used to paint the fence. That paintbrush has a length of 1 meter. Joke paints the fence by brushing either horizontally or vertically, but the paint is expensive so Joke wants to minimize the number of paintbrush stroke. On each stroke, the paintbrush will make either a horizontal or vertical line. Also, the paintbrush must be touching the fence for the entire duration of the stroke. Joke also does not want to paint previously panted segment before. Help Joke to find the minimum number of stroke until the entire fence is covered with paint.

    Input

    First line contains a number N, the number of boards on the fence. The second line contains N numbers, A1, A2, A3 ... An representing the height of each board.

    Output

    Minimum number of stroke to paint the entire fence.

    Sample Input 1

    5
    2 2 1 2 1

    Sample Output 1

    3
    

    Sample Input 2

    2
    2 2

    Sample Output 2

    2
    

    Sample Input 3

    1
    5

    Sample Output 3

    1

    题意:又N个宽度为1的相邻围栏,每个有高度a[i],现在有一把宽度为1的刷子,可以横着刷或者竖着刷,问最少多少次刷完。

    思路:对于每个区间,我们的最优情况的全部竖着刷, 或者横着刷全部公有的部分,其他的继续讨论。

    由于每次最小横着刷一个,所以讨论次数不超过N。复杂度低于O(N^2)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=5010;
    const int inf=1e9+7;
    int a[maxn];
    int solve(int L,int R,int H)
    {
        if(L==R) return 1;
        int ht=inf,res=0,r;
        for(int i=L;i<=R;i++) ht=min(ht,a[i]);
        for(int i=L;i<=R;i++){
            if(ht==a[i]) continue;
            r=i;
            while(r<R&&a[r+1]>ht) r++;
            res+=solve(i,r,ht);
            i=r+1;
        }
        res=min(R-L+1,res+ht-H); return res;
    }
    int main()
    {
        int N,i;
        scanf("%d",&N);
        for(i=1;i<=N;i++) scanf("%d",&a[i]);
        printf("%d
    ",solve(1,N,0));
        return 0;
    } 
  • 相关阅读:
    二维数组输出10行杨辉三角
    二维数组的练习----求和
    数组的异常及处理
    二维数组在内存中的结构
    Ubuntu系统中安装Mercurial 以支持hg
    什么是插补、直线插补、联动与插补
    压力表(负压表...)
    常用接近开关的原理和分类
    VMware Ubuntu安装详细过程
    Redis+Spring缓存实例(windows环境,附实例源码及详解)
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9090995.html
Copyright © 2011-2022 走看看