版权声明:本文为博主原创文章,未经博主同意不得转载。

https://blog.csdn.net/y990041769/article/details/37935237

题目:codeforces 448CPainting Fence

题意:n个1* a [ i ] 的木板。把他们立起来,变成每一个木板宽为1长为 a [ i ] 的栅栏。如今要给栅栏刷漆。刷子宽1,每一刷子能够刷随意长,如今让你求最少须要多少刷子?

分析:题目看似没有头绪。细致分析的话事实上非常简单

首先,我们假如每次都刷一个木板。即一竖行,那么须要n次刷完。可见这是一个ans的最大值。就是最差的情况下我这样刷最多为n刷。

其次:假设我们选择一横行的刷,而n个木板中最短的为min。那么我们能够花min刷。把他们都刷成a [ i ] - min的高度。那么剩下来的栅栏又变成了開始的情况,我们能够在选择前面不为0的x个,继续按上面的方法刷,可见是一个递归调用就可以。

要特别注意的是前面的条件,就是刷x个木板。最多用x刷,假设某一次求得大于x,那么取x,这样就非常easy了。

事实上能够归结为:事实上最初的思想能够归结为优先横刷,其次竖刷(假设竖刷花费更小),注意一定要细致。

代码:

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 5500;
int a[N];
int tmp=0,ans=0;
void solve(int s,int t)
{
int ma=-1,mi=0x3f3f3f3f;
for(int i=s;i<t;i++)
{
if(a[i]>ma)
ma=a[i];
if(a[i]<mi)
mi=a[i];
}
if(ma==mi){
tmp+=min(mi,t-s);
return ;
}
for(int i=s;i<t;i++)
a[i]-=mi;
mi=min(mi,t-s);
tmp+=mi;
for(int i=s;i<t;i++)
{
if(a[i]>0)
{
for(int j=i;j<t;j++) ///枚举连续不为0段
{
if(a[j]==0 || j==(t-1) &&a[j]>0)
{
if(j==(t-1) && a[j]>0)
j++;
int kk=tmp;
solve(i,j);
if(tmp-kk>(j-i)){ //推断假设求得的值比直接一行一行刷更大的话,取更小的
tmp=kk+(j-i);
}
i=j;break;
}
}
}
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
tmp=0;
ans=0;
solve(0,n);
printf("%d\n",min(n,tmp));
}
return 0;
}

codeforces 448CPainting Fence的更多相关文章

  1. CodeForces 363B Fence

    Fence Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ...

  2. codeforces B.Fence 解题报告

    题目链接:http://codeforces.com/problemset/problem/363/B 题目意思:给定整数n和k,需要从n个数中找出连续的k个数之和最小,输出这连续的k个数中的第一个数 ...

  3. codeforces 232D Fence

    John Doe has a crooked fence, consisting of n rectangular planks, lined up from the left to the righ ...

  4. Codeforces 659G Fence Divercity dp

    Fence Divercity 我们设a[ i ] 为第 i 个围栏被切的最靠下的位置, 我们发现a[ i ] 的最大取值有一下信息: 如果从i - 1过来并在 i  结束a[ i ] = min(h ...

  5. codeforces 349B Color the Fence 贪心,思维

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

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

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

  7. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence

    http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...

  8. Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题

    A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...

  9. Codeforces Round #346 (Div. 2) G. Fence Divercity dp

    G. Fence Divercity 题目连接: http://www.codeforces.com/contest/659/problem/G Description Long ago, Vasil ...

随机推荐

  1. open, creat - 用来 打开和创建 一个 文件或设备

    SYNOPSIS 总览 #includ e <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int o ...

  2. 在虚拟机Linux中安装VMTools遇到的问题-小结

    总结: 遇到的问题:No support for locale: zh_CN.utf8 可能的解决方法:1.sudo dpkg-reconfigure locale (重新配置?) 2.上一步失败,提 ...

  3. 读书笔记一、pandas之series

    转自 # 直接传入一组数据 from pandas import Series, DataFrame obj = Series([4, 2, 3]) obj 0 4 1 2 2 3 dtype: in ...

  4. Docker之安装缺省指令

    Docker 中有些指令不存在,需要额外的安装,这里做下安装记录. 更新软件源中的所有软件列表 apt-get update 安装 ifconfig apt install net-tools 安装 ...

  5. 安装phpredis扩展以及phpRedisAdmin工具

    先从phpredis的git拿到最新的源码包:wget https://github.com/nicolasff/phpredis/archive/master.tar.gz 然后解压到进入目录:ta ...

  6. 1.Linux安装redis

    Linux安装redis 操作系统是Centos7 1.下载压缩包 2.解压 3.编译 4.启动redis 5.设置redis.conf和防火墙端口开放,外网可以访问 1.下载压缩包 下载地址:htt ...

  7. php range()函数 语法

    php range()函数 语法 作用:创建一个包含指定范围的元素的数组.dd马达哪家好 语法:range(low,high,step) 参数: 参数 描述 low  必需.规定数组的最低值. hig ...

  8. 富文本编辑器tinymce支持从word复制粘贴保留格式和图片的插件wordpaster

    tinymce是很优秀的一款富文本编辑器,可以去官网下载.https://www.tiny.cloud 这里分享的是它官网的一个收费插件powerpaste的旧版本源码,但也不影响功能使用. http ...

  9. UPDATE 在不同数据库中的使用方式

    MYSQL 中update 表一 set Gmoney = 表二.列名 from 表一,表二 where 表一.EMPID = 表二.EMPID举例:update table1 set table1. ...

  10. hdu 5511 Minimum Cut-Cut——分类讨论思想+线段树合并

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5511 题意:割一些边使得无向图变成不连通的,并且恰好割了两条给定生成树上的边.满足非树边两段一定在给定生成 ...