1. 程式人生 > >SPOJ:House Fence(分治&DP)

SPOJ:House Fence(分治&DP)

day 一個 prev 現在 wan stroke AS make ica

"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\n",solve(1,N,0));
    return 0;
} 

SPOJ:House Fence(分治&DP)