`
Simone_chou
  • 浏览: 182736 次
  • 性别: 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;
}

 

分享到:
评论

相关推荐

    C#,栅栏油漆算法(Painting Fence Algorithm)的源代码

    C#,栅栏油漆算法(Painting Fence Algorithm)的源代码 给定一个有n根柱子和k种颜色的围栏,找出油漆围栏的方法,使最多两个相邻的柱子具有相同的颜色。因为答案可以是大的,所以返回10^9+7的模。

    electric-fence 源码

    electric fence是Linux下的C语言内存越界访问检测工具,可检测如下两种情况:1、内存访问越界 2、使用释放后续的内存 使用方法: 1、make编译得到libefence.a静态库 2、在gcc编译时增加 -L . -lefence的选项

    fence2.11破解版

    fence2.11破解版 很好用的桌面图标管理工具支持WIN7

    usaco_fence3解题

    usaco第五章fence3的解题代码,供算法初学者参考

    fence 桌面管理工具

    Fence2.0以後開始收費了,雖然網上有相應的破解方法,但還是比較繁瑣,不如直接來個綠色安裝版簡單。工具嘛,就要簡單易用。

    Fence And Stairs(栏杆楼梯)中文版

    Fence And Stairs(栏杆楼梯)中文版

    electric fence

    内存调试工具electric fence. software that overruns the boundaries of a malloc()memory allocation, software that touches a memory allocation that has been released by free(). Unlike other malloc() ...

    fence破解方法(2.12,2.11都适用)

    fence破解方法(2.12,2.11都适用)

    Ground Via Fence.pdf

    印刷電路板電路佈局(PCB Layout)上,常會在高速訊號或重要訊號的兩側,並行兩條地線並且打上許多接地導穿孔,此種做法稱為 Ground Via Fence(接地圍)。

    Pku acm 第3253题 Fence Repair 代码

    Pku acm 第3253题 Fence Repair 代码,有详细的注释,哈夫曼数

    fence-virt-0.2.3-24.el6.x86_64.rpm

    fence-virt-0.2.3-24.el6.x86_64.rpm fence-virt-0.2.3-24.el6.x86_64.rpm

    fence-agents-4.0.15-13.el6.x86_64.rpm

    fence-agents-4.0.15-13.el6.x86_64.rpm fence-agents-4.0.15-13.el6.x86_64.rpm

    桌面整理工具fence

    个人见到的最好的免费桌面整理工具,看看人家老外是怎么做工具的,UI没的说

    fence2.1 官方版

    很好用哦!非常有名气的Fences软件,可以将桌面上的图标分类整理,非常简单易用.这对于爱把文件放桌面的人非常实用.

    桌面栅栏fence2.01

    非常有名气的Fences软件,可以将桌面上的图标分类整理,非常简单易用.这对于爱把文件放桌面的人非常实用.

    fence-agents-3.1.5-17.el6.i686.rpm

    fence-agents-3.1.5-17.el6.i686.rpm是工具centos包 。

    fence-virtd-checkpoint-0.2.3-9.el6.i686.rpm

    fence-virtd-checkpoint-0.2.3-9.el6.i686.rpm是工具centos包 。

    Fence最好的图标工具

    Fences是著名的Stardock公司新推出的一款软件,用来分类和组织桌面上的图标。它可以将不同的图标放到不同的半透明“容器”当中,你还可以移动和拉伸这个“容器”。这样将图标分类以后,你的桌面再也不会看起来一团糟...

    专业桌面整理软件 Stardock Fence 3破解版

    fences 3是一款能够帮助用户进行桌面图标管理的图标自动整理工具,用户在使用时可以将桌面图标拖动到围起的区域内使其加入到图标分组中,从而有效管理桌面图标的排列方式,也可以在桌面上自定义矩形区域用来创建新的...

    fence-virt-0.2.3-9.el6.i686.rpm

    fence-virt-0.2.3-9.el6.i686.rpm是工具centos包 。

Global site tag (gtag.js) - Google Analytics