递归。分治。

C. Painting Fence
time limit per test

1 second

memory limit per test

512 megabytes

input

standard input

output

standard output

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.

Input

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

Output

Print a single integer — the minimum number of strokes needed to paint the whole fence.

Sample test(s)
input
5
2 2 1 2 1
output
3
input
2
2 2
output
2
input
1
5
output
1
Note

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 <vector> using namespace std; const int maxn=5500; typedef long long int LL;
typedef pair<int,int> pII; LL f[maxn],n; LL solve(int l,int r)
{
LL mx=9999999999;
vector<pII> pi;
for(int i=l;i<=r;i++)
mx=min(mx,f[i]);
bool flag=false;
int duan[2];
for(int i=l;i<=r;i++)
{
f[i]-=mx;
if(f[i])
{
if(flag==false)
{
flag=true;
duan[0]=i;
}
}
else if(f[i]==0)
{
if(flag==true)
{
flag=false;
duan[1]=i-1;
pi.push_back((make_pair(duan[0],duan[1])));
}
}
}
if(flag==true)
{
flag=false;
duan[1]=r;
pi.push_back((make_pair(duan[0],duan[1])));
}
LL digui=0;
int sz=pi.size();
for(int i=0;i<sz;i++)
{
digui+=solve(pi[i].first,pi[i].second);;
}
return min((LL)(r-l+1),digui+mx);
} int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>f[i];
cout<<solve(1,n)<<endl;
return 0;
}

Codeforces 448 C. Painting Fence的更多相关文章

  1. Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目

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

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

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

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

  5. codeforces 256 div2 C. Painting Fence 分治

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

  6. CodeForces 448

    A:Rewards: 题目链接:http://codeforces.com/problemset/problem/448/A 题意:Bizon有a1个一等奖奖杯,a2个二等奖奖杯,a3个三等奖奖杯,b ...

  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 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  9. Codeforces 484E Sign on Fence(是持久的段树+二分法)

    题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...

随机推荐

  1. 《Javascript权威指南》学习笔记之十九--HTML5 DOM新标准---处理文档元信息和管理交互能力

    一.了解DOM 1.DOM是Document Object Model的缩写,即文档对象类型,是文档在内存中的表示形式,是一个应用程序接口,定义了文档的逻辑结构以及一套訪问和处理文档的方法. 2.HT ...

  2. 学习-短信的上行(MO)和下行(MT)详解

    基础知识: SP服务提供商: 通常是指在移动网内运营增值业务的社会合作单位, 它们建立与移动网络建立相连的服务平台, 为手机用户提供一系列信息服务, 如:娱乐.游戏.短信.彩信.WAP.彩铃.铃声下载 ...

  3. Javascript 判断网页横竖屏

    本篇文章由:http://xinpure.com/javascript-to-determine-page-anyway-screen/ Html5 流行至今,自适应的网站已经多如牛毛,但是横竖屏的切 ...

  4. 【转】打造自己的视频会议系统 GGMeeting(附送源码)

    原文地址:http://www.cnblogs.com/justnow/p/4487201.html 自从在博客园发布开源即时通信系统GG(QQ高仿版)以来,结识了很多做IM的朋友,然后我和我的伙伴们 ...

  5. VSCode开发工具下载

    VSCode集成多语言和插件,方便开发和代码管理. 请到此处下载:https://code.visualstudio.com/Download

  6. unity3d控制主摄像头移动

    这篇很水,因为就只有一行代码==: Camera.main.transform.Translate (Vector3.forward); 这行代码控制主摄像头向前移动,其它的如下: up:向上 dow ...

  7. SQL&EF优化第一篇 各种情况下的性能测试之count函数篇

    测试环境  mssql 08  +win7    数据 30W条 二〇一六年十月二十九日 09:04:43 结论:1>主键> *>可空列    推测未论证: 根据情况优先选择 顺便提 ...

  8. TCP网络传输, 数据类型的问题

    转载: http://blog.csdn.net/highfly591/article/details/45309239 1.采用TCP传输时, 应用层为什么要做超时重传: tcp保证数据可靠传输,传 ...

  9. 利用email.net设置网络代理发邮件

    近期在做一个项目,客户的内部网络须要设置代理才干收发邮件,本来一个简单的东西突然变得复杂了,在baidu搜索了非常久都没找到适合的组件.baidu就像个废物一样,没办法仅仅能去yahoo搜,结果在微软 ...

  10. SVN安装配置及安全注意事项

    两个脚本: svn遍历脚本.zip wooyun上也是已经有非常多的svn泄露网站信息的事件,有的甚至由此导致整个服务器沦陷: WooYun: [盛大180天渗透纪实]第四章.SVN猎手 (某站SVN ...