CF-448C Painting Fence 分治
Painting fence
题意
乍一看以为是之前做过的一道单调队列优化的DP,不是。
也是有n块木板,每个木板宽1米,有一个高度ai,现在要把他们刷成橘色,给了你一个宽一米的刷子,你可以横着刷,或者竖着刷,问最少需要刷几下才能将所有的木板着色。
思路
对于一个区间[l,r]的木板来说,第一步要么把所有的木板都竖着刷,要么把最低的木板横着刷一遍,问题变为区间所有的木板减去最短木板的高度之后,刷分为的两个小区间的次数和+最短木板高度。两者取最小值即可。分治
代码
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+10;
const int mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3f;
const double eps=1e-14;
int arr[N];
int solve(int l,int r)
{
if(l>r) return 0;
int pos,minn=inf;
for(int i=l;i<=r;i++)
{
if(arr[i]<minn)
{
minn=arr[i];
pos=i;
}
}
for(int i=l;i<=r;i++) arr[i]-=minn;
return min(r-l+1,solve(l,pos-1)+solve(pos+1,r)+minn);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&arr[i]);
printf("%d\n",solve(1,n));
return 0;
}
CF-448C Painting Fence 分治的更多相关文章
- cf 448c Painting Fence
http://codeforces.com/problemset/problem/448/C 题目大意:给你一个栅栏,每次选一横排或竖排染色,求把全部染色的最少次数,一个点不能重复染色. 和这道题有点 ...
- codeforces 256 div2 C. Painting Fence 分治
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
- 448C - Painting Fence(分治)
题意:给出宽为1高为Ai的木板n条,排成一排,每次上色只能是连续的横或竖并且宽度为1,问最少刷多少次可以使这些木板都上上色 分析:刷的第一步要么是所有的都竖着涂完,要么是先横着把最矮的涂完,如果是第一 ...
- painting fence - 分治 - Codeforces 448c
2017-08-02 14:27:18 writer:pprp 题意: • 每块木板宽度均为1,高度为h[i] • n块木板连接为宽度为n的栅栏 • 每次可以刷一横或一竖(上色) • 最少刷多少次可以 ...
- Codeforces 448C Painting Fence:分治
题目链接:http://codeforces.com/problemset/problem/448/C 题意: 有n个木板竖着插成一排栅栏,第i块木板高度为a[i]. 你现在要将栅栏上所有地方刷上油漆 ...
- [Codeforces 448C]Painting Fence
Description Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion ...
- C. Painting Fence 分治
memory limit per test 512 megabytes input standard input output standard output Bizon the Champion i ...
- Codeforces 448C Painting Fence(分治法)
题目链接:http://codeforces.com/contest/448/problem/C 题目大意:n个1* a [ i ] 的木板,把他们立起来,变成每个木板宽为1长为 a [ i ] 的栅 ...
- Code Forces 448C Painting Fence 贪婪的递归
略有上升称号,最近有很多问题,弥补啊,各类竞赛滥用,来不及做出了所有的冠军.这个话题 这是一个长期记忆的主题.这是不是太困难,基本技能更灵活的测试,每次我们来看看这个问题可以被删除,处理然后分段层,贪 ...
随机推荐
- LCA Nearest Common Ancestors (很典型的例题)
A rooted tree is a well-known data structure in computer science and engineering. An example is show ...
- 腾讯云集群服务部署mysql并挂载到服务器
一.背景 由于现在大部分的应用都是运行在云服务器上的,而现在大多数文章都是主要写如何在服务器上使用docker去运行mysql,比较少有介绍云服务器上的.再加上现在k8s比较火爆,而云厂商大多数都提供 ...
- Redis Linux安装+配置
1.进入指定目录,下载资源(也可本地下载后复制到指定目录) wget http://download.redis.io/releases/redis-5.0.5.tar.gz 2.解压到指定目录 ta ...
- shiro:集成Spring(四)
基于[加密及密码比对器(三)]项目改造 引入相关依赖环境 shiro-spring已经包含 shiro-core和shiro-web 所以这两个依赖可以删掉 <!--shiro继承spring依 ...
- MySQL笔记总结-DQL语言
DQL语言 基础查询 一.语法 select 查询列表 from 表名; 二.特点 1.查询列表可以是字段.常量.表达式.函数,也可以是多个 2.查询结果是一个虚拟表 三.示例 1.查询单个字段 se ...
- python 规范篇 如何合理使用 assert
assert 的合理使用,可以增加代码的健壮度,同时也方便了程序出错时开发人员的定位排查. 什么是 assert? Python 的 assert 语句,可以说是一个 debug 的好工具,主要用于测 ...
- Pytorch手写线性回归
pytorch手写线性回归 import torch import matplotlib.pyplot as plt from matplotlib.animation import FuncAnim ...
- php--static用法
static关键字声明一个属性或方法是和类相关的,而不是和类的某个特定的实例相关,因此,这类属性或方法也称为“类属性”或“类方法”. 如果访问控制权限允许,可不必创建该类对象而直接使用类名加两个冒号“ ...
- php 对象的调用和引入
直接上实例: 定义: <?php namespace app\php; class a { ; public function index() { echo "; } static f ...
- Ubuntu忘记超级用户root密码,重新设置密码 转载
原文链接:https://blog.csdn.net/weixin_37909391/article/details/80691601 Ubuntu版本:Ubuntu 16.04.3 LTS 1启动系 ...