`
Simone_chou
  • 浏览: 184622 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Painting Fence(分治)

    博客分类:
  • CF
 
阅读更多
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 integersa1, a2, ..., an (1 ≤ ai ≤ 109).

Output

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

Sample test(s)
input
5
2 2 1 2 1
output
3
input
2
2 2
output
2
input
1
5
output
1
Note

In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.

In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.

In the third sample there is only one plank that can be painted using a single vertical stroke.

 

       题意:

       给出 n(0 ~ 5000),后给出 n 个数(1 ~ 10 ^ 9),代表有 n 块板,每块板 长为 ai,宽为 1,全部板并排打竖放着,现有一个刷子宽 1,可以一次性打横扫或者打竖扫,扫的时候不能有间隔,问最少扫多少次能将所有板都扫一遍。

 

       思路:

       分治法。整体来看,有两种方法,要不全部都打竖扫一遍,要不把共同最小值打横扫一遍 + 剩余不连续块的最小扫描数。而求剩余不连续块的最小扫描数的时候也同样也是运用这样的方法,故可以运用递归求解,当剩余 1 X ai 的时候则返回 1。

 

       AC:

       递归函数传过去的是左右区间边界,还有上一次所减去的最小值数 k 。所以下一次迭代求最小值的时候,要减去上一次的最小值 k,同时比较的时候也是这样。传下下次迭代的时候,最小值要加上上一次的最小值。注意这些就没有问题了。

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int MAX = 1000000005;

int num[5005];

int solve(int l, int r, int k) {
        int Min = MAX;

        if (l == r) return 1;

        for (int i = l; i <= r; ++i) {
                Min = min(Min, num[i] - k);
        }

        int ans = 0;
        for (int i = l; i <= r; ++i) {
                if (num[i] - k > Min) {
                        int ll = i, rr = i + 1;

                        while (rr <= r && num[rr] - k > Min) ++rr;
                        --rr;

                        ans += solve(ll, rr, Min + k);
                        i = rr + 1;
                }
        }

        return min(r - l + 1, Min + ans);
}

int main() {

        int n;
        scanf("%d", &n);

        for (int i = 1; i <= n; ++i)
                scanf("%d", &num[i]);

        printf("%d\n", solve(1, n, 0));

        return 0;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics