CF 448C Painting Fence(分治)
来源:互联网 发布:淘宝延长收货怎么操作 编辑:程序博客网 时间:2024/06/05 10:01
参考:点击打开链接
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of ai meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
The first line contains integer n (1 ≤ n ≤ 5000) — the number of fence planks. The second line contains n space-separated integersa1, a2, ..., an (1 ≤ ai ≤ 109).
Print a single integer — the minimum number of strokes needed to paint the whole fence.
52 2 1 2 1
3
22 2
2
15
1
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>typedef long long LL;using namespace std;const int maxn=5500;int h[maxn],n;int solve(int l,int r){ int len=r-l+1; int minn=h[l]; for(int i=l;i<=r;i++) if(h[i]<minn) minn=h[i]; for(int i=l;i<=r;i++) h[i]-=minn; int ans=minn; for(int i=l,k;i<=r;i++) { if(h[i]) { int ll=i; int k=i+1; while(h[k]&&k<=r) k++; int rr=k-1; ans+=solve(ll,rr); } } if(ans>len) ans=len; return ans;}int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) scanf("%d",&h[i]); printf("%d\n",solve(1,n)); } return 0;}
- CF 448C Painting Fence(分治)
- codeforces 448C Painting Fence(分治)
- CF 448C Painting Fence
- CF 448C Painting Fence
- cf #256 (Div. 2) C. Painting Fence 分治
- CodeForces 448C - Painting Fence(分治)
- #256 C. Painting Fence 分治
- codeforces 448C C. Painting Fence(分治+dp)
- codeforces 448C C. Painting Fence(分治+dp)
- codeforces 448C C. Painting Fence(分治+dp)
- 448C. Painting Fence
- Painting Fence[分治]
- Codeforces 448 C. Painting Fence
- Codeforces 448C. Painting Fence
- CodeForces 448-C. Painting Fence
- codeforces 448C Painting Fence
- CodeForces-448C Painting Fence
- Codeforces 448C Painting Fence
- javascript prototype、jjquery的extend和fn.extend
- HTMLParser使用
- UVA11402 - Ahoy, Pirates!(线段树)
- xp 更改系统信息
- 排列问题
- CF 448C Painting Fence(分治)
- 自定义定时器(Timer)
- jsp的自定义标签库
- CSS页面布局基础4——元素可见性、溢出和元素重叠问题
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--客户端对象模型和REST APIs概览 REST和ODATA
- 第二章线性表知识导图
- sql中的split方法
- 用C实现链表的初始化,以及在表头插入节点,以及遍历整个列表
- [Codevs 1230]元素查找(手写哈希表)