Codeforces Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)
解题报告
给篱笆上色,要求步骤最少,篱笆怎么上色应该懂吧,。,刷子能够在横着和竖着刷,不能跳着刷,,,
假设是竖着刷,应当是篱笆的条数,横着刷的话。就是刷完最短木板的长度,再接着考虑没有刷的木板,,。
递归调用,,。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define inf 999999999999999
using namespace std;
long long n,num[5010],tt;
void dfs(long long s,long long t)
{
long long ma=0,mi=inf;
int i,j;
for(i=s; i<=t; i++)
{
if(ma<num[i])
ma=num[i];
if(mi>num[i])
mi=num[i];
}
if(mi==ma)
{
tt+=min(mi,t-s+1);
return ;
}
for(i=s; i<=t; i++)
num[i]-=mi;
tt+=min(mi,t-s);
for(i=s; i<=t; i++)
{
if(num[i]>0)
{
for(j=i; j<=t; j++)
{
if(num[j]==0||(j==t&&num[j]>0))
{
long long kk=tt;
if(j==t&&num[j]>0)
{
dfs(i,j);
if(tt-kk>(j-i+1))
tt=kk+(j-i+1);
}
else
{
dfs(i,j-1);
if(tt-kk>(j-i))
tt=kk+(j-i);
}
i=j;
break;
}
}
}
}
}
int main()
{
int i,j;
scanf("%lld",&n);
for(i=1; i<=n; i++)
scanf("%lld",&num[i]);
dfs(1,n);
printf("%lld\n",min(tt,n));
return 0;
}
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.
Codeforces Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)的更多相关文章
- 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 Round #256 (Div. 2) C. Painting Fence (搜索 or DP)
[题目链接]:click here~~ [题目大意]:题意:你面前有宽度为1,高度给定的连续木板,每次能够刷一横排或一竖列,问你至少须要刷几次. Sample Input Input 5 2 2 1 ...
- Codeforces Round #256 (Div. 2) 题解
Problem A: A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #256 (Div. 2) D. Multiplication Table(二进制搜索)
转载请注明出处:viewmode=contents" target="_blank">http://blog.csdn.net/u012860063?viewmod ...
- Codeforces Round #256 (Div. 2) B. Suffix Structures(模拟)
题目链接:http://codeforces.com/contest/448/problem/B --------------------------------------------------- ...
- Codeforces Round #256 (Div. 2)
A - Rewards 水题,把a累加,然后向上取整(double)a/5,把b累加,然后向上取整(double)b/10,然后判断a+b是不是大于n即可 #include <iostream& ...
- Codeforces Round #256 (Div. 2/B)/Codeforces448B_Suffix Structures(字符串处理)
解题报告 四种情况相应以下四组数据. 给两字符串,推断第一个字符串是怎么变到第二个字符串. automaton 去掉随意字符后成功转换 array 改变随意两字符后成功转换 再者是两个都有和两个都没有 ...
随机推荐
- 蜂鸟A20开发板刷 cubietruck 的 SD 卡固件
美睿视讯 为蜂鸟A20准备的 MerriiLinux 功能非常简陋.所以能用上主流的 debian 或者 LUbuntu 就可以说是非常迫切的需求了.蜂鸟A20(Merrii Hummingbird ...
- C语言运算符的优先级
熟悉C语言的同学都知道,C语言众多的运算符及繁琐难记的优先级总是搞得我们这些C初学者头大.那么本文就 对C语言中所有的运算符进行汇总,并对其优先级进行一定的介绍. 这里虽然对所有C运算符的优先级进行了 ...
- [SQL]一个删选数据的例子,使用GROUP、DISTINCT
今天遇到的问题,纠结了一上午,终于解决了.在此记录下来,自我认为还有很多类似的问题都可以套用这段代码. 需求描述: 一个表MyImage,列有:号码ID,路径PATH 如: ID PATH 1 C ...
- 关于各种排列(dfs)
代码一:数字有重复: #include <cstdio> ],arr[]={,,,}; void dfs(int v){ if(v >= n){ ;i<n;i++) print ...
- Apple Swift学习资料汇总
今年的苹果开发者大会(WWDC)上,公布了ios8的几个新特性,其中包括引入了群聊功能,支持第三方输入法等功能.但更让开发者感兴趣的莫过于Swift语言的发布了. Swift是apple自创的一门转为 ...
- 单调旋转数组的TopK问题
问题描述:输入一个单调旋转后的数组,求该数组中的第k小的元素. 分析:很多人看到这个题目会有点懵,可能读者不知道什么是旋转数组,我先解释下两个概念, 旋转数组的定义:把一个数组的前几项元素移动到数组的 ...
- UVA 1614 - Hell on the Markets
题意: 输入n个数,第i个数ai满足1≤ai≤i.对每个数添加符号,使和值为0. 分析: 排序后从最大的元素(假设为k)开始,凑出sum/2即可.用去掉了k的集合,一定可以凑出sum/2 - a[k] ...
- 如何灵活利用免费开源图标字体-IcoMoon篇
http://www.zhangxinxu.com/wordpress/2012/06/free-icon-font-usage-icomoon/
- HasMap
您还未登录 ! 登录 注册 论坛首页 → Java企业应用论坛 → 深入理解HashMap 全部 Hibernate Spring Struts iBATIS 企业应用 Lucene SOA Java ...
- n条直线的最多交点
#include <iostream>using namespace std;int main(){int i,n;while(cin>>n){if(n==0||n==1) c ...