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. tar 打包带软连接的文件

    打包普通文件夹,压缩带参数z,创建tar.gz tar -cvf ./tmp/SK_Aug_camera.tar ./gap_40_5 但是文件夹里含有软连接,带参数 h tar -cvhf ./tm ...

  2. random模块 参生随机数

    记得要import random模块 随机整数: >>> import random >>> random.randint(0,99) 21 随机选取0到100间的 ...

  3. HttpClient使用小结

    使用HttpClient发送请求.接收响应很简单,只要如下几步即可. 1. 创建HttpClient对象. 2. 如果需要发送GET请求,创建HttpGet对象:如果需要发送POST请求,创建Http ...

  4. BottomNavigationView的使用

    BottomNavigationView的使用 废话少说, 先看东西 依赖 implementation 'com.android.support:design:26.1.0' 布局 //用这个控件需 ...

  5. new和delete的三种形式详解

    一.new操作符.delete操作符 class String { public: String(const char *str="") { if(str== NULL) { da ...

  6. Centos 下 mysql 安装过程

    1. 检查系统自带的Mysql,并卸载自带的版本 [root@iZ2366ycl7sZ config]# yum list installed |grep mysql mysql.x86_64 5.1 ...

  7. [原]nginx 一下快一下慢的问题

    在本机用thinkphp建了一个小网站,没任何问题,发布到云空间,就出现访问很慢的情况,而且是一下快一下慢,奇数次快,偶数次慢 换了一台win10的笔记本,情况一样,更新了phpstudy更新了thi ...

  8. String in Java

  9. 转:c# Linq 的分页[转]

    转:http://www.cnblogs.com/leleroyn/archive/2008/05/14/1196811.html 很多学习Linq的朋友肯定有自己所不同的方法,考虑这个问题我所想到的 ...

  10. sublime_text3常用配置

    安装(pojie)不再赘述. 一.设置字体与编码 preferences->Settings->Settings-User,在大括号中输入如下内容: “font_size”:16.0, “ ...