2017-08-02 14:27:18

writer:pprp

题意:

  • 每块木板宽度均为1,高度为h[i]
  • n块木板连接为宽度为n的栅栏
  • 每次可以刷一横或一竖(上色)
  • 最少刷多少次可以使得栅栏被全部上色
  • 1 ≤ n ≤ 5000

算法分析:可以横着刷,可以竖着刷,横着刷是为了减小竖着刷的次数

    采用分治,每个分治中都取横着刷和竖着刷两者的最小值


代码及说明如下:

#include <iostream>
#include <queue> using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
int n;
int a[maxn]; //在递归算法中不要用n,应该考虑的是在每个部分,而不能只是在第一个递归中的角标
int dfs(int l,int r)
{
int MIN = INF;
int cnt = ; //找到所有木板中最短的那个
for(int i = l ; i <= r; i++)
{
MIN = min(MIN, a[i]);
} //将数目加上最短板长度
cnt += MIN; //所有的木板减去这个长度
for(int i = l; i <= r; i++)
{
a[i] -= MIN;
} int left = l; // 分段递归解决问题
for(int i = l; i <= r; i++)
{
if(a[i] == )
{
cnt +=dfs(left,i-);
left = i+ ;
}
} //最后一段,需要一个判断
if(left <= r)
cnt += dfs(left,r); return min(cnt,r-l+);
} int main()
{
cin >> n; for(int i = ; i <= n ; i++)
{
cin >> a[i];
} cout << dfs(,n) << endl; return ;
}

painting fence - 分治 - Codeforces 448c的更多相关文章

  1. codeforces 256 div2 C. Painting Fence 分治

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

  2. CF-448C Painting Fence 分治

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

  3. C. Painting Fence 分治

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

  4. 【题解】Painting Fence

    [题解]Painting Fence 分治模板.贪心加分治.直接\(O(n^2logn)\)分治过去.考虑一块联通的柱形是子问题的,是递归的,贪心分治就可.记得对\(r-l+1\)取\(min\). ...

  5. Codeforces Round #256 (Div. 2) C. Painting Fence(分治贪心)

    题目链接:http://codeforces.com/problemset/problem/448/C C. Painting Fence time limit per test 1 second m ...

  6. Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目

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

  7. CF448C Painting Fence (分治递归)

    Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...

  8. Codeforces 448 C. Painting Fence

    递归.分治. . . C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input ...

  9. Codeforces Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)

    解题报告 给篱笆上色,要求步骤最少,篱笆怎么上色应该懂吧,.,刷子能够在横着和竖着刷,不能跳着刷,,, 假设是竖着刷,应当是篱笆的条数,横着刷的话.就是刷完最短木板的长度,再接着考虑没有刷的木板,,. ...

随机推荐

  1. ef AddDays报错

    ef func写法,在语句中不能使用adddays方法 )); 这样写就是不行 可以改为: ); 下面是我的一个案例,虽然到了最后都没有实现功能! public List<ContractBud ...

  2. SharePoint BI

    本篇博客主要针对SharePoint BI整体结构进行整理,为读者分析几种Sharepoint BI场景 先附一张自己做的结构图:

  3. php获取本地IP

    function get_local_ip() { $preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3 ...

  4. Fluent Ribbon 第六步 StartScreen

    上一节,介绍了Toolbar的主要功能,说明了ToolBar的一些最基本用法,这一节,介绍Ribbon的一个重要功能startScreen, startScreen软件第一次启动,呈现的界面. 由于R ...

  5. cross-origin HTTP request

    w https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS A resource makes a cross-ori ...

  6. pdb

    core code: import pdb pdb.set_trace() 单步执行并进入:s 单步执行并不进入:n 下一断点:c 当前位置:where 从当前函数返回:r 退出:q pdb comm ...

  7. git "Could not read from remote repository.Please make&n

    git "Could not read from remote repository.Please make sure you have the correct access rights. ...

  8. spring 实现定时任务

    spring实现定时任务超级简单.比使用quartz简单,比使用timer强大.如下是一个简单的springboot任务,启用了定时任务 @SpringBootApplication@Componen ...

  9. pandas数据结构和介绍第一天

    pandans另种主要的数据结构Series和DateFranme 1,Series 仅由一组数据就而已产生简单的Series 2)Series 有index和values属性,表达索引对象 3)设置 ...

  10. 聚合的安全类导航、专业的安全知识学习平台——By Me:)

    以“基于对抗的安全研发”为初衷,让大家在工作中始终有安全意识.安全思维和安全习惯,几年前自己搭建了面向公司内部全员的安全晨报.现在站在“用户“的角度回头看看,觉得科目设计等很多方面都还有很多的不足: ...