cf 448c Painting Fence
http://codeforces.com/problemset/problem/448/C
题目大意:给你一个栅栏,每次选一横排或竖排染色,求把全部染色的最少次数,一个点不能重复染色。
和这道题有点像,不过可以竖着。
考虑横着涂一次的情况,那么有两个显而易见的事实。
1.这次涂色长度必须尽可能大。
2.在这次涂色区域的下方,必定都是横着涂的。
所以,对于一串栅栏h 1 , h 2 , ... , h n ,如果要横着涂,就必定要从底向上涂min{h 1 , h 2 , ... , h n }次。这样以后,h 1 , h 2 , ... , h n 就会分成若干不连通的子局面。
那么显然可以设计一个分治的算法,时间复杂度为O(N 2 ):
令\(Solve(l, r, h)\)代表\([l, r]\)这段栅栏,已经从下向上涂了h格的答案。
令 \(h'= min{h_1, h_2 , ... , h_n }\),那么:
\(Solve(l, r, h) = min{r - l + 1, \sum solve(u, v, h')|[u, v]为分割出的子局面}\)
边界情况:l = r 时,答案显然为 1。
以后static不能乱用(在递归中),,,
// It is made by XZZ
#include<cstdio>
#include<algorithm>
#define Fname "color"
using namespace std;
#define rep(a,b,c) for(rg int a=b;a<=c;a++)
#define drep(a,b,c) for(rg int a=b;a>=c;a--)
#define erep(a,b) for(rg int a=fir[b];a;a=nxt[a])
#define il inline
#define rg register
#define vd void
typedef long long ll;
il ll gi(){
rg ll x=0;bool flg=0;rg char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')flg=1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return flg?-x:x;
}
ll n,h[5010];
il ll Doit(ll l,ll r,ll _h){
if(l==r)return 1;
ll m=2e18;
rep(i,l,r)m=min(m,h[i]);
ll ans=m-_h;
rep(i,l,r)
if(h[i]!=m){
ll j;j=i;
while(j!=r&&h[j+1]!=m)++j;
ans+=Doit(i,j,m),i=j+1;
}
return min(ans,r-l+1);
}
int main(){
freopen(Fname".in","r",stdin);
freopen(Fname".out","w",stdout);
n=gi();
rep(i,1,n)h[i]=gi();
printf("%lld\n",Doit(1,n,0));
return 0;
}
cf 448c Painting Fence的更多相关文章
- [Codeforces 448C]Painting Fence
Description Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion ...
- Code Forces 448C Painting Fence 贪婪的递归
略有上升称号,最近有很多问题,弥补啊,各类竞赛滥用,来不及做出了所有的冠军.这个话题 这是一个长期记忆的主题.这是不是太困难,基本技能更灵活的测试,每次我们来看看这个问题可以被删除,处理然后分段层,贪 ...
- 448C - Painting Fence(分治)
题意:给出宽为1高为Ai的木板n条,排成一排,每次上色只能是连续的横或竖并且宽度为1,问最少刷多少次可以使这些木板都上上色 分析:刷的第一步要么是所有的都竖着涂完,要么是先横着把最矮的涂完,如果是第一 ...
- Codeforces 448C Painting Fence(分治法)
题目链接:http://codeforces.com/contest/448/problem/C 题目大意:n个1* a [ i ] 的木板,把他们立起来,变成每个木板宽为1长为 a [ i ] 的栅 ...
- Codeforces 448C Painting Fence:分治
题目链接:http://codeforces.com/problemset/problem/448/C 题意: 有n个木板竖着插成一排栅栏,第i块木板高度为a[i]. 你现在要将栅栏上所有地方刷上油漆 ...
- Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
- CF448C Painting Fence (分治递归)
Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...
- 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 ...
随机推荐
- Kubernetes 学习2 k8s基础概念
一.架构描述 1.基本架构 2.pod ,有两类 a.自主式pod 自我管理的,创建之后,任然是需要提交给API Server,API Server接受之后然后由调度器调度到指定的 node节点,由n ...
- Python 基于request库的get,post,delete,封装
# coding=utf-8 import json import requests class TestApi(object): """ /* @param: @ses ...
- [USACO08JAN]电话线$Telephone \ \ Lines$(图论$+SPFA+$ 二分答案)
#\(\mathcal{\color{red}{Description}}\) \(Link\) 给定一个图,请你求出在把其中自由选择的\(k\)条的权值都置为零的情况下,图中\(1-N\)最短路上的 ...
- Linux 带宽、CPU、内存占用情况
iftop 查看带宽占用情况(总)yum install -y iftop 安装iftopnethogs 查看进程流量 curl http://218.5.73.233:8060/ip.php 查看出 ...
- Gradle Goodness: Excluding Tasks for Execution
In Gradle we can create dependencies between tasks. But we can also exclude certain tasks from those ...
- Maven创建Web项目、、、整合SSM框架
自己接触ssm框架有一段时间了,从最早的接触新版ITOO项目的(SSM/H+Dobbu zk),再到自己近期来学习到的<淘淘商城>一个ssm框架的电商项目.用过,但是还真的没有自己搭建过, ...
- CentOS7安装及相关配置转载
1. 怎样在 CentOS 7.0 上安装和配置 VNC 服务器 https://linux.cn/article-5335-1.html 2. 安装完最小化 RHEL/CentOS 7 后需要做的 ...
- Java Activiti 工作流引擎 springmvc SSM 流程审批 后台框架源码
1.模型管理 :web在线流程设计器.预览流程xml.导出xml.部署流程 2.流程管理 :导入导出流程资源文件.查看流程图.根据流程实例反射出流程模型.激活挂起 3.运行中流程:查看流程信息.当前任 ...
- Ajax缓存
一.Ajax缓存的好处 这种设计使客户端对一些静态页面内容的请求,比如图片,css文件,js脚本等,变得更加快捷,提高了页面的响应速度,也节省了网络通信资源. 二.Ajax缓存的不足 Ajax缓存虽然 ...
- linux 学习第十四天(Apache安装、基于ip、基于域名、基于端口配置)
一.虚拟主机 A.基于IP地址 B.基于域名 C.基于端口号 复习yum仓库挂载 mkdir /media/cdrom mount /dev/cdrom /media/cdrom/ vim /et ...