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中, 连续最长长度大于 ...
随机推荐
- windows域相关
查看域角色: netdom query fsmo
- 面向对象知识点之statickeyword的使用
<?php /*由static定义的属性和方法称为静态成员和静态方法.static定义的属性和方法是属于类的,在对象之间共享.*/ /*比如能够通过定义一个静态变量来统计类实例化了多少个对象*/ ...
- hp-ux 集群,内存 小记
-----查看hp 集群状态信息 # cmviewcl -v CLUSTER STATUS dbsvr up NODE ...
- 【Linux】xshell连接中断后就无法连接虚拟机中的Linux
具体情景是这样的: 在使用Linux的时候,本来一直好好的,突然就断了,我去百度了一番,网上的说法有千万种 有的说:是由于防火墙的问题 有的说:是由于Linux与其他ip冲突造成 ... 说法千万种, ...
- jBoss无法通过IP地址访问,只能用localhost\127.0.0.1访问
http://feng88724.iteye.com/blog/263211 JBOSS版本:4.2.2GA 症状:服务器无法通过IP地址去访问,只能用127.0.0.1或者localhost来访问. ...
- C语言指针的易错点
1.内存泄漏:申请的堆内存没有释放. 2.内存污染:前面非法操作使用内存(没有报错),后面写着写着就出错.如下代码: 当结构体中只有划线部分代码时,在编译器中编写不会报错,但此时已经造成非法操作内存, ...
- 设备管理器里“SM总线控制器”、“其它PCI桥设备”驱动有问题
WinXP重装系统后设备管理器里面出现黄色问号.各自是"SM总线控制器"和"其它PCI桥设备",主板是七彩虹的,芯片组是 geForce 7025的,南桥是 n ...
- 创建cocos2d-x+lua项目
1> 创建cocos2d-x+lua项目 进入到cocos2d-x-2.1.5\tools\project-creator文件夹运行下面命令: python create_project ...
- 在ubuntu上使用Virtual-Box安装Mininet
使用Virtual-Box安装Mininet看上去简单,但其中也暗藏许多坑.我自己装了多次Mininet,但每次都有缺陷: mininet访问不了网络 用宿主机访问不了mininet虚拟机 最后,终于 ...
- (2.0)Smali系列学习之Smali语法
一.smali的包中信息 .class public Lcom/aaaaa;.super Lcom/bbbbb;.source "ccccc.java" 1.它是com.aaa ...