codeforces 448CPainting Fence
版权声明:本文为博主原创文章,未经博主同意不得转载。
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的更多相关文章
- CodeForces 363B Fence
Fence Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ...
- codeforces B.Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/363/B 题目意思:给定整数n和k,需要从n个数中找出连续的k个数之和最小,输出这连续的k个数中的第一个数 ...
- codeforces 232D Fence
John Doe has a crooked fence, consisting of n rectangular planks, lined up from the left to the righ ...
- Codeforces 659G Fence Divercity dp
Fence Divercity 我们设a[ i ] 为第 i 个围栏被切的最靠下的位置, 我们发现a[ i ] 的最大取值有一下信息: 如果从i - 1过来并在 i 结束a[ i ] = min(h ...
- 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中, 连续最长长度大于 ...
- 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的数 维护 ...
- 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 ...
- Codeforces Round #346 (Div. 2) G. Fence Divercity dp
G. Fence Divercity 题目连接: http://www.codeforces.com/contest/659/problem/G Description Long ago, Vasil ...
随机推荐
- 82-基于Xilinx Virtex-5LXT FPGA的四路光纤PCIE卡(4路光纤卡)
基于Xilinx Virtex-5LXT FPGA的四路光纤PCIE卡(4路光纤卡) 1.板卡特点: 1) 主芯片采用Xilinx Virtex-5LXT FPGA. 前面板提供4路光纤接口,速率可 ...
- 一个奇怪的问题:Last_Errno: 1264 Error 'Out of range value for column 0x322E36343030
场景环境: 1. 主从都是:Server version: 5.7.16-log MySQL Community Server (GPL) 2.操作系统:CentOS release 6.7 (Fin ...
- windows下zookeeper集群安装
windows下zookeeper单机版安装,见:https://www.cnblogs.com/lbky/p/9867899.html 一:zookeeper节点为什么是奇数个? 单机模式的zk进程 ...
- Linux安装mysql5.6.33
1.下载mysql安装包: 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:我这里选择的5.6.33,通用版,lin ...
- handy源码阅读(二):EventsImp类
EventsImp用于完成事件的处理. class EventsImp { EventBase* base_; PollerBase* poller_; std::atomic<bool> ...
- Linux find过滤掉没有查看权限的文件
参考:https://blog.csdn.net/sinat_39416814/article/details/84993424 https://www.jianshu.com/p/2b056e1c0 ...
- Maven安装本地jar包至本地repository
1.安装jar包 Maven 安装 JAR 包的命令是: mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -Dartifa ...
- 前端每日实战:6# 视频演示如何用纯 CSS 绘制一颗闪闪发光的璀璨钻石
效果预览 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/qYqwQp 可交互视频教程 此视频是可以交 ...
- Step2 - How to: Implement a Windows Communication Foundation Service Contract
This is the second of six tasks required to create a basic Windows Communication Foundation (WCF) se ...
- 使用代理IP、高匿IP、连接失败
先百度一下,什么是代理IP 我们使用代理IP就是因为某些站点会屏蔽我们的IP,所以我们要动态的更换代理IP. 代理IP: 其中我们首先选择国内的IP,国外的一般都比较慢,其次不要选择如{新疆乌鲁木齐} ...