CodeForces 574D Bear and Blocks
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample.
Limak will repeat the following operation till everything is destroyed.
Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time.
Limak is ready to start. You task is to count how many operations will it take him to destroy all towers.
The first line contains single integer n (1 ≤ n ≤ 105).
The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers.
Output
Print the number of operations needed to destroy all towers.
Examples
6
2 1 4 6 2 2
3
7
3 3 3 1 3 3 3
2
Note
The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color.
After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
OJ-ID:
CodeForce 574D
author:
Caution_X
date of submission:
20191019
tags:
dp
description modelling:
给定一个有小正方形组成的不规则图形,现在进行操作:每次都消去暴露在外面的小正方形,问需要几次操作才能消去所有小正方形?
major steps to solve it:
1.dp1[i]:=以第i列为最后一列从前往后可以得到的连续上升子序列
2.dp2[i]:=以第i列为最后一列从后往前可以得到的连续上升子序列
备注:此处连续上升子序列是指可以找到排列成阶梯状的连续上升格子,例如小正方形排列为3 3 3 ,此时仍有连续上升子序列1(3) 2(3) 3
3.对每一列dp取min(dp1,dp2),ans=max(dp[i])
AC code:
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int dp1[],dp2[],a[];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++) {
scanf("%d",&a[i]);
}
dp1[n-]=dp2[n-]=dp1[]=dp2[]=;
for(int i=;i<n;i++) {
dp1[i]=;
if(a[i]>a[i-]) {
dp1[i]=dp1[i-]+;
}
else {
if(dp1[i-]+<=a[i])
dp1[i]=dp1[i-]+;
else dp1[i]=a[i];
}
}
for(int i=n-;i>=;i--) {
dp2[i]=;
if(a[i]>a[i+]) {
dp2[i]=dp2[i+]+;
}
else {
if(dp2[i+]+<=a[i])
dp2[i]=dp2[i+]+;
else dp2[i]=a[i];
}
}
// for(int i=0;i<n;i++) cout<<dp1[i]<<' ';
// cout<<endl;
// for(int i=0;i<n;i++) cout<<dp2[i]<<' ';
// cout<<endl;
int ans=;
for(int i=;i<n;i++){
ans=max(ans,min(dp1[i],dp2[i]));
}
printf("%d\n",ans);
return ;
}
CodeForces 574D Bear and Blocks的更多相关文章
- Codeforces 573B Bear and Blocks
http://codeforces.com/problemset/problem/573/B 题目大意: 给出n个连续塔,每个塔有高度hi,每次取走最外层的块,问需要多少次操作能够拿光所有的块. 思 ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) B. Bear and Blocks 水题
B. Bear and Blocks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/573/pr ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- Codeforces 385B Bear and Strings
题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...
- Codeforces 680D Bear and Tower of Cubes 贪心 DFS
链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...
- Codeforces 385C Bear and Prime Numbers(素数预处理)
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
- [Codeforces 639F] Bear and Chemistry (Tarjan+虚树)(有详细注释)
[Codeforces 639F] Bear and Chemistry(Tarjan+虚树) 题面 给出一个n个点,m条边的无向图(不保证连通,可能有自环和重边),有q次询问,每次询问给出p个点和q ...
- 【32.89%】【codeforces 574D】Bear and Blocks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces Round #318 (Div. 2) D Bear and Blocks (数学)
不难发现在一次操作以后,hi=min(hi-1,hi-1,hi+1),迭代这个式子得到k次操作以后hi=min(hi-j-(k-j),hi-k,hi+j-(k-j)),j = 1,2,3... 当k ...
随机推荐
- 浏览器记住密码的自动填充Input问题完美解决方案
1.input 前from和input占位隐藏 <form style="width:0;height:0;display:none;"> <input type ...
- RAID 2.0 技术(块虚拟化技术)
RAID 2.0 技术(块虚拟化技术) RAID 2.0 技术(块虚拟化技术),该技术将物理的存储空间划分为若干小粒度数据块,这些小粒度的数据块均匀的分布在存储池中所有的硬盘上,然后这些小粒度的数据块 ...
- TCP协议 - 可靠性
在前篇文章中介绍了TCP协议的三大特性,其中可靠性是依赖一系列的机制,如:校验和,分组发送,超时重传,流量控制得到保证. 一.数据交互 TCP在交互数据时,采用多种机制保证可靠性,同时也保证TCP的性 ...
- SPA项目开发之动态树、表格、分页
思路: 1.准备好后台(左侧树,带分页的文章查询) 2.将左侧树的数据绑定到elementui中的menu标签上 3.新增一个自定义组件用来展示文章列表的 4.绑定elementui提供的分页组件来完 ...
- PHP语法入门以及变量
1PHP语法入门 1.1PHP是编译型语言 编译语言和解释语言的区别在于是否保存最终的可执行程序. 1.2PHP定界符 因为PHP是脚本语言,所以需要定界符 <?php e ...
- python xlwt模块简介
一.基础类介绍 1.工作簿类Workbook简介: import xlwt class Workbook(object0): ''' 工作簿类,使用xlwt创建excel文件时,首先要实例化此类的对象 ...
- LooseVersion()使用及.__version__版本号的获取
我简单看了distutils库,但发现目前还用不到,感觉有些复杂.因此我简单复制了别人的介绍,如下: Distutils可以用来在Python环境中构建和安装额外的模块.新的模块可以是纯Python的 ...
- 前端开发JS——jQuery常用方法
jQuery基础(三)- 事件篇 1.jQuery鼠标事件之click与dbclick事件 click方法用于监听用户单击操作,dbclick方法用于监听用户双击操作,这两个方法用法及其类似,所以 ...
- FCC---Create Movement Using CSS Animation---设计一个盒子上下左右移动,结合animation, @keyframe, position (上下左右的offset)
When elements have a specified position, such as fixed or relative, the CSS offset properties right, ...
- 实验吧简单的SQL注入1,简单的SQL注入
接上面一篇博客. 实验吧简单的sql注入1 题目连接 http://ctf5.shiyanbar.com/423/web/ 同样,直接输入 1加个但引号,结果下面有返回错误, ...