http://codeforces.com/problemset/problem/448/C


题目大意:给你一个栅栏,每次选一横排或竖排染色,求把全部染色的最少次数,一个点不能重复染色。

这道题有点像,不过可以竖着。

考虑横着涂一次的情况,那么有两个显而易见的事实。

1.这次涂色长度必须尽可能大。

2.在这次涂色区域的下方,必定都是横着涂的。

所以,对于一串栅栏h 1 , h 2 , ... , h n ,如果要横着涂,就必定要从底向上涂min⁡{h 1 , h 2 , ... , h n }次。这样以后,h 1 , h 2 , ... , h n 就会分成若干不连通的子局面。

那么显然可以设计一个分治的算法,时间复杂度为O(N 2 ):

令\(Solve(l, r, h)\)代表\([l, r]\)这段栅栏,已经从下向上涂了h格的答案。

令 \(h'= min⁡{h_1, h_2 , ... , h_n }\),那么:

\(Solve(l, r, h) = ⁡min{⁡r - l + 1, \sum solve(u, v, h')⁡|⁡[u, v]为分割出的子局面⁡⁡}\)

边界情况:l = r 时,答案显然为 1。

以后static不能乱用(在递归中),,,

// It is made by XZZ
#include<cstdio>
#include<algorithm>
#define Fname "color"
using namespace std;
#define rep(a,b,c) for(rg int a=b;a<=c;a++)
#define drep(a,b,c) for(rg int a=b;a>=c;a--)
#define erep(a,b) for(rg int a=fir[b];a;a=nxt[a])
#define il inline
#define rg register
#define vd void
typedef long long ll;
il ll gi(){
rg ll x=0;bool flg=0;rg char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')flg=1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return flg?-x:x;
}
ll n,h[5010];
il ll Doit(ll l,ll r,ll _h){
if(l==r)return 1;
ll m=2e18;
rep(i,l,r)m=min(m,h[i]);
ll ans=m-_h;
rep(i,l,r)
if(h[i]!=m){
ll j;j=i;
while(j!=r&&h[j+1]!=m)++j;
ans+=Doit(i,j,m),i=j+1;
}
return min(ans,r-l+1);
}
int main(){
freopen(Fname".in","r",stdin);
freopen(Fname".out","w",stdout);
n=gi();
rep(i,1,n)h[i]=gi();
printf("%lld\n",Doit(1,n,0));
return 0;
}

cf 448c Painting Fence的更多相关文章

  1. [Codeforces 448C]Painting Fence

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

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

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

  3. 448C - Painting Fence(分治)

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

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

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

  5. Codeforces 448C Painting Fence:分治

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

  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 Round #256 (Div. 2) C. Painting Fence 或搜索DP

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

  9. Codeforces Round #256 (Div. 2) C. Painting Fence

    C. Painting Fence Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Ch ...

随机推荐

  1. 【[HEOI2016/TJOI2016]字符串】

    码农题啊 上来先无脑一个\(SA\)的板子,求出\(SA\)和\(het\)数组 我们只需要从\(sa[i]\in[a,b]\)的所有\(i\)中找到一个\(i\)使得\(sa[i]\)和\(rk[c ...

  2. session.upload_progress.enabled开启的问题

    exp.php的内容,存在文件包含 <?php include($_GET['lfi']); $key = ini_get("session.upload_progress.prefi ...

  3. adb命令篇 (转载)

    转自:https://www.cnblogs.com/ailiailan/p/7896534.html 1.抓log方法  (bat文件)  mkdir D:\logcat set /p miaosh ...

  4. 8、Web Service-IDEA-jaxws规范下的 spring整合CXF

    前提:开发和之前eclipse的开发有很大的不同! 1.服务端的实现 1.新建项目 此时创建的是web项目 2.此时创建的项目是不完整的需要开发人员手动补充完整 3.对文件夹的设置(满满的软件使用方法 ...

  5. Java50道经典习题-程序23 求岁数

    题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁.问第4个人岁数,他说比第3个人大2岁.问第三个人,又说比第2人大两岁.问第2个人,说比第1个人大两岁.最后问第一个人,他说是10岁.请问 ...

  6. No.5 - 纯 CSS 制作绕中轴旋转的立方体

    body{ background-color: #000; margin:; padding:; } main{ perspective: 800px; } .cube{ transform-styl ...

  7. ethereumjs/browser-builds

    https://github.com/ethereumjs/browser-builds ethereumjs - Browser Builds This repository contains br ...

  8. java 静态相关内容

    一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序来调用的时候,需要使用静态方法,这种代码 ...

  9. PHPStorm自定义主题配置

    1.下载喜欢的主题 官方下载地址:下载 2.将.icls主题文件放到PHPStorm的配置中 windows下主题位置:C:\Users\Administrator\.PhpStorm2017.3\c ...

  10. CDN的作用与基本过程

     转载请注明出处: leehao.me 或 https://blog.csdn.net/lihao21/article/details/52808747 简介 CDN,Content Distribu ...