Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) B. Bear and Blocks (技巧dp 难想)
B. Bear and Blocks
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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
input
Copy
6
2 1 4 6 2 2
output
Copy
3
input
Copy
7
3 3 3 1 3 3 3
output
Copy
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.
题意:给定一堆方块,每执行一次操作可以把最外层的方块全部去掉,如上图所示,问要多少次操作才能让这些方块全部消失
首先每次操作之后消失的是什么先弄清楚,左右两排肯定没了,然后就是中间的顶上部分,中间的部分要消失一定必须等到左右都消失了才行,除非它自己每进行一次操作自减1的速度比左右两排消失的速度快,
这道题其实有个地方很神奇,分左右dp然后取最小值,虽然不太理解,但是我递推了一遍感觉是没错的,总之这道题我觉得我没有理解的十分透彻...主要是正确性不是很有把握...
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
template <typename T> inline void read(T& x)
{
x=0;char ch=getchar();ll f=1;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}x*=f;
}
ll n,a[100005],dpz[100005],dpy[100005],ans;
int main()
{
cin>>n;
for(rg i=1;i<=n;i++)cin>>a[i];
for(rg i=1;i<=n;i++)dpz[i]=min(a[i],dpz[i-1]+1);
for(rg i=n;i>=1;i--)dpy[i]=min(a[i],dpy[i+1]+1);
for(rg i=1;i<=n;i++)ans=max(ans,min(dpz[i],dpy[i]));
cout<<ans<<endl;
return 0;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 又一个自动生成项目目录组件tree-cli,快速生成Readme项目结构
- 用Vue CLI创建uni-app,摆脱HBuilder,npm命令行运行及发布
- codeforces 1133D(map+精度控制)
- 1024程序员节 | 我在腾讯自研数据库,我为技术代言
- jasmine spyOn的单步调试
- codeforces 1363C(优先队列)
- 系统与应用异常定位诊断
- SQL优化终于干掉了“distinct”
- SAP Spartacus delivery mode页面Cannot find control with的错误消息
- codeforces 1349A(数学)
- 阔别两年,webpack 5 正式发布了!
- SAP Spartacus delivery mode continue button enable与否的逻辑
- codeforces 1342C(前缀和)
- leetcode-寻找两个正序数组的中位数
- Shell脚本常用命令一览