题意:给出宽为1高为Ai的木板n条,排成一排,每次上色只能是连续的横或竖并且宽度为1,问最少刷多少次可以使这些木板都上上色

分析:刷的第一步要么是所有的都竖着涂完,要么是先横着把最矮的涂完,如果是第一种,那么ans等于n,如果是第二种,那么ans=最矮的高度+被刷掉最矮的后,新的几段不连续木板最小上色次数,所以用分治可以解决这个问题

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+10;
int h[maxn],n;
int paint(int st,int en)
{
if(st>en)return 0;
int _min=1e9,c1=0;
for(int i=st; i<=en; i++)
_min=min(h[i],_min);
c1+=_min;
for(int i=st; i<=en; i++)
h[i]-=_min;
int s=st;
for(int i=st; i<=en; i++)
{
if(i==en&&h[i]!=0)
{
c1+=paint(s,i),s=i+1;
}
else if(h[i]==0)
{
c1+=paint(s,i-1),s=i+1;
}
}
return min(en-st+1,c1);
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(int i=1; i<=n; i++)cin>>h[i];
cout<<paint(1,n)<<endl;
return 0;
}

  

448C - Painting Fence(分治)的更多相关文章

  1. CF-448C Painting Fence 分治

    Painting fence 题意 乍一看以为是之前做过的一道单调队列优化的DP,不是. 也是有n块木板,每个木板宽1米,有一个高度ai,现在要把他们刷成橘色,给了你一个宽一米的刷子,你可以横着刷,或 ...

  2. codeforces 256 div2 C. Painting Fence 分治

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

  3. painting fence - 分治 - Codeforces 448c

    2017-08-02 14:27:18 writer:pprp 题意: • 每块木板宽度均为1,高度为h[i] • n块木板连接为宽度为n的栅栏 • 每次可以刷一横或一竖(上色) • 最少刷多少次可以 ...

  4. Codeforces 448C Painting Fence:分治

    题目链接:http://codeforces.com/problemset/problem/448/C 题意: 有n个木板竖着插成一排栅栏,第i块木板高度为a[i]. 你现在要将栅栏上所有地方刷上油漆 ...

  5. [Codeforces 448C]Painting Fence

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

  6. C. Painting Fence 分治

    memory limit per test 512 megabytes input standard input output standard output Bizon the Champion i ...

  7. Codeforces 448C Painting Fence(分治法)

    题目链接:http://codeforces.com/contest/448/problem/C 题目大意:n个1* a [ i ] 的木板,把他们立起来,变成每个木板宽为1长为 a [ i ] 的栅 ...

  8. cf 448c Painting Fence

    http://codeforces.com/problemset/problem/448/C 题目大意:给你一个栅栏,每次选一横排或竖排染色,求把全部染色的最少次数,一个点不能重复染色. 和这道题有点 ...

  9. Code Forces 448C Painting Fence 贪婪的递归

    略有上升称号,最近有很多问题,弥补啊,各类竞赛滥用,来不及做出了所有的冠军.这个话题 这是一个长期记忆的主题.这是不是太困难,基本技能更灵活的测试,每次我们来看看这个问题可以被删除,处理然后分段层,贪 ...

随机推荐

  1. oracle 压力测试工具benchmarksql

    TPC-C测试 TPC-C 于 1992 年 7 月 23 日认可为新的基准测试.TPC(Transaction Processing Performance Council,事务处理性能委员会)-C ...

  2. lua table排序报错与解决

    lua table排序 table的sort函数 比如按照大小进行排序,下面这种写法在某些情况下可能会排序错误,甚至报invalid order function for sorting table. ...

  3. June 14. 2018 Week 24th Thursday

    Good friends, good books, and a sleepy conscience: this is the ideal life. 拥有益友.良书和一颗宁静的内心:这就是理想的生活. ...

  4. 枚举应用demo

    package com.xx; public enum PositionEM{ ONE(1,"领导"), TWO(2,"员工"); private Long v ...

  5. 028实现strStr()

    #pragma once #include "000库函数.h" /*********************自解**************/ //使用算法中的find 12ms ...

  6. 【teradata】强制解锁

    使用加锁用户释放锁,也可以用其它用户使用如下语句强制解锁 Release lock (pdm_data ),Override

  7. 关于this的理解

    var o = { a:10, b:{ a:12, fn:function(){ console.log(this.a); //undefined console.log(this); //windo ...

  8. A. On The Way to Lucky Plaza 概率 乘法逆元

    A. On The Way to Lucky Plaza time limit per test 1.0 s memory limit per test 256 MB input standard i ...

  9. Tomcat配置(部分知识点)

    1.<Server>元素,shutdown属性表示关闭Server的指令:port属性表示Server接收shutdown指令的端口号,设为-1可以禁掉该端口 2.Connector的主要 ...

  10. springboot中使用freemarker生成word文档并打包成zip下载(简历)

    一.设计出的简历模板图以及给的简历小图标切图         二.按照简历模板图新建简历word文件 :${字段名},同时将图片插入到word中,并将建好的word文件另存为xml文件:    三.直 ...