Codeforces 448 C. Painting Fence
递归。分治。
。
。
1 second
512 megabytes
standard input
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.
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.
5
2 2 1 2 1
3
2
2 2
2
1
5
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 <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的更多相关文章
- Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目
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(分治贪心)
题目链接:http://codeforces.com/problemset/problem/448/C C. Painting Fence time limit per test 1 second m ...
- 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 ...
- codeforces 256 div2 C. Painting Fence 分治
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
- CodeForces 448
A:Rewards: 题目链接:http://codeforces.com/problemset/problem/448/A 题意:Bizon有a1个一等奖奖杯,a2个二等奖奖杯,a3个三等奖奖杯,b ...
- CF448C Painting Fence (分治递归)
Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces 484E Sign on Fence(是持久的段树+二分法)
题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...
随机推荐
- Ubuntu 11.04 安装后要做的20件事情
转自:http://www.cnbeta.com/articles/141137.htm #1 不喜欢Unity? 切换到Ubuntu gnome 经典桌面 注销unity桌面环境,然后选择登录环境为 ...
- CSRF攻击原理及测试方法
CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,该攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在并未授权的情况下执 ...
- javascript解析器(引擎)
The JavaScript interpreter in a browser is implemented as a single thread. javascript 引擎在浏览器中作为单线程实现 ...
- 1 bootstrap table null默认显示为 - 要查源码 2 记一个很无语的bug
本来返回的json 3个true 7个false的 结果显示10个true 因为本来是好的 结果判断的问题 给全部赋值true了
- json再理解
一.Json的简单介绍 从结构上看,所有的数据最终都可以分成三种类型: 第一种类型是scalar(标量),也就是一个单独的string(字符串)或数字(numbers),比如“北京”这个单独的词. 第 ...
- chrome 浏览器设置字体大小,方便调试
H5开发的时候,有时候明明字体没有那么大,但在chrome中看上去依然很大.就像型号不对一样,但在手机端是正常的. 这是因为chrome浏览器设置了默认字体.只需要设置一下即可. 「chrome:/ ...
- 经典.net面试题目(转载)
1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保 ...
- Tensorflow - Tutorial (7) : 利用 RNN/LSTM 进行手写数字识别
1. 经常使用类 class tf.contrib.rnn.BasicLSTMCell BasicLSTMCell 是最简单的一个LSTM类.没有实现clipping,projection layer ...
- C#多线程方法同步
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- mysql 两列互转
1.mysql 一张表两列互转 UPDATE tm_position as a,tm_position as b SET a.gps_longitude = b.gps_latitude,a.gps_ ...