cf 448c Painting Fence
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的更多相关文章
- [Codeforces 448C]Painting Fence
Description Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion ...
- Code Forces 448C Painting Fence 贪婪的递归
略有上升称号,最近有很多问题,弥补啊,各类竞赛滥用,来不及做出了所有的冠军.这个话题 这是一个长期记忆的主题.这是不是太困难,基本技能更灵活的测试,每次我们来看看这个问题可以被删除,处理然后分段层,贪 ...
- 448C - Painting Fence(分治)
题意:给出宽为1高为Ai的木板n条,排成一排,每次上色只能是连续的横或竖并且宽度为1,问最少刷多少次可以使这些木板都上上色 分析:刷的第一步要么是所有的都竖着涂完,要么是先横着把最矮的涂完,如果是第一 ...
- Codeforces 448C Painting Fence(分治法)
题目链接:http://codeforces.com/contest/448/problem/C 题目大意:n个1* a [ i ] 的木板,把他们立起来,变成每个木板宽为1长为 a [ i ] 的栅 ...
- Codeforces 448C Painting Fence:分治
题目链接:http://codeforces.com/problemset/problem/448/C 题意: 有n个木板竖着插成一排栅栏,第i块木板高度为a[i]. 你现在要将栅栏上所有地方刷上油漆 ...
- Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
- CF448C Painting Fence (分治递归)
Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...
- 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 ...
- 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 ...
随机推荐
- Guava包学习-Multimap
它和上一章的MultiSet的继承结果很相似,只不过在上层的接口是Multimap不是Multiset. Multimap的特点其实就是可以包含有几个重复Key的value,你可以put进入多个不同v ...
- fastjson反序列化JdbcRowSetImpl
Gadget com.sun.rowset.JdbcRowSetImpl setAutoCommit() -> connect() -> InitialContext.lookup() p ...
- 好用的纯CSS加载动画-spinkit
首先放一个css spinkit <style> .loaders{ width: 100%; height: 100%; padding: 100px; box-sizing: bor ...
- .net根据经纬度获取地址(百度api)
private string GetAddress(string lng, string lat) { try { string url = @"http://api.map.baidu.c ...
- shell一次性执行多条命令
1.每个命令之间用;隔开说明:各命令的执行给果,不会影响其它命令的执行.换句话说,各个命令都会执行,但不保证每个命令都执行成功. 2.每个命令之间用&&隔开说明:若前面的命令执行成功, ...
- 定义一个类Point,代表一个点,public属性有x和y,方法有显示点坐标 show(),构造函数有两个参数分别给x,y赋值,在main方法中构造两个对象,再创建一方法(getMiddle)为取两个点构成线段的中点的坐标,参数为2个点对象,调用此方法后得到一个新的点,编写Application,显示该对象的坐标值。
这个题让我更加明白了类创建对象的实质 代码中用到:1.对象作形参;2.对象作返回值 以下用代码具体分析: class Point1{ public double x; public double y; ...
- 修改网卡MAC地址后出现问题:device eth0 does not seem to be present, delaying initialization
修改网卡MAC地址后出现问题:device eth0 does not seem to be present, delaying initialization 1.修改网卡对应的文件,将配置文件中 ...
- UIWebView 获取网页标题
- (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *urlString = webView.request.URL.absolu ...
- spring boot 输入参数统一校验
1 引入spring boot validate maven 依赖 <!-- 验证 --> <dependency> <groupId>org.hiberna ...
- 1006.Sign in and Sign out(25)—PAT 甲级
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...