题目链接:http://codeforces.com/problemset/problem/448/C

题目大意:用宽度为1的刷子刷墙,墙是一长条一长条并在一起的。梳子可以一横或一竖一刷到底。求刷完整面墙最少要几次。

关键思路:很有意思,我们会发现,一座墙,从最短的那根往下的矩形区域,全刷完后会形成多座“小山”,这和最先要刷的墙形状是一样的。所以问题就可以进行分治了。

代码如下:

//图像类比
#include<iostream>
#include<algorithm>
using namespace std;
int fence[10000] = { 0 }; int solve(int s, int n) {
int minh = fence[s];
int times = 0;
if (n == 1)return 1;
for (int i = 0; i < n; i++)
fence[s + i] <= minh ? minh = fence[s + i] : minh;
for (int i = 0; i < n; i++)
fence[s + i] -= minh;
times += minh;
int start = 0, end = 0;
for (int i = 0; i <= n; i++) {
if ((fence[s + i] != 0) && (fence[s + i - 1] == 0))start = s + i;
if ((fence[s + i] == 0) && (fence[s + i - 1] != 0)) {
end = s + i - 1;
times += solve(start, end - start + 1);
continue;
}
} return min(times, n);
} int main() {
int Total;
cin >> Total; int i = 0;
for (i = 0; i < Total; i++)
cin >> fence[i]; cout << solve(0, Total)<<endl;
//system("Pause");
return 0; }

  

CF448C [Painting Fence]递归分治的更多相关文章

  1. CF448C Painting Fence (分治递归)

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

  2. CF-448C Painting Fence 分治

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

  3. 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 ...

  4. CF448C Painting Fence (贪心分治)

    题面 \(solution:\) 一道蛮水的分治题,但思想很不错(虽然我还是非常天真的以为是积木大赛原题,并且居然还有30分) 看到这个题目,根据贪心的一贯风格,我们肯定能想到将整个栅栏的下面某部分直 ...

  5. 448C - Painting Fence(分治)

    题意:给出宽为1高为Ai的木板n条,排成一排,每次上色只能是连续的横或竖并且宽度为1,问最少刷多少次可以使这些木板都上上色 分析:刷的第一步要么是所有的都竖着涂完,要么是先横着把最矮的涂完,如果是第一 ...

  6. CF448C Painting Fence

    传送门 Descriptionzed 最近总是受到 Farmer 的困扰,因此他在自家的门前插了一排栅栏以防农气的入侵.栅栏由 N 个竖条栅栏横向组成,每个竖条栅栏宽度为 1.过了一段时间,zed 觉 ...

  7. Codeforces 448C Painting Fence:分治

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

  8. codeforces 256 div2 C. Painting Fence 分治

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

  9. Codeforces 448 C. Painting Fence

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

随机推荐

  1. 关联规则算法---Eclat算法

    Eclat算法 与fp-growth 和apriori算法不同,Eclat算法加入了倒排的思想,具体就是将事务数据中的项作为key,每个项对应的事务ID作为value. 原输入数据为 tid item ...

  2. 漂亮的HTML表格 - ebirdfighter的日志 - 网易博客

    一个像素边框的表格: Info Header 1 Info Header 2 Info Header 3 Text 1A Text 1B Text 1C Text 2A Text 2B Text 2C ...

  3. Ubuntu“无法解析或打开软件包的列表或是状态文件”的解决办法。_StarSasumi_新浪博客

    Ubuntu"无法解析或打开软件包的列表或是状态文件"的解决办法. (2011-04-30 14:56:14) 转载▼ 标签: ubuntu apt 分类: Ubuntu/Linu ...

  4. (中等) POJ 2886 Who Gets the Most Candies? , 反素数+线段树。

    Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...

  5. D Vitamin - the wonder vitamin

    原文 Be healthier and happier by spending time in the sun In the dead of winter,we don't typically thi ...

  6. js设置文本框只能输入数字

    JS判断只能是数字和小数点 1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'' ...

  7. cookie设置

    问题: cookie设置好后,谷歌浏览器下-->只有本页面有值,但是在其它浏览器下正常. $.cookie("userName",$("#loginName&quo ...

  8. T4模板使用

    本例使用的数据库是Northwind 1.新建tt文本模板customer.tt 2. 修改customer.tt内容为 <#@ template debug="false" ...

  9. leetcode day6

    [13]Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...

  10. SQL SERVER 2000 数据恢复(分离数据库+附加数据库)

    一.分离数据库     SQL Server 2000允许分离数据库的数据和事务日志文件,然后将其重新附加到同一台或另一台服务器上.分离数据库将从SQL Server 删除数据库,所以当点击“分离数据 ...