链接:戳这里
D. Bear and Blocks time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard 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
6
2 1 4 6 2 2
output
3
input
7
3 3 3 1 3 3 3
output
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.
题意:
给出n个高度的n座积木,现在要拆积木,如果一块积木能拆需要满足的条件是
这块积木的上下左右至少有一块是空着的,而且拆积木的时候,当前所有能拆的积木只需要一次操作就能拆除
问最少需要拆多少次可以拆完
思路:
一开始想着模拟过程,但是给出的如果是1e5个1e9高度的积木,那么需要的次数是 1e5*(1e5/2)会T
换个思路,我们可以统计出每列积木的最小操作次数,怎么统计呢?
双向dp,先从左往右统计当前第i列从左到右至少需要的操作次数
转移方程也就是 dp[i]=min{i,a[i],dp[i-1]+1}
i :表示从左往右一列一列消去需要的操作次数
a[i]:表示从上往下一块一块消去需要的操作次数
dp[i-1]+1:表示前一块已经消去,那么第i块也只需要dp[i-1]+1次
同理,在统计一遍从右往左的最小!
这样我就知道了当前的第i列从左往右和从右往左的最小操作次数,每次选择当前两个值的最小值
再去用ans更新这n个值得最大值也就是答案了
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
int a[100100];
int L[100100],R[100100];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
mst(L,127);
mst(R,127);
L[0]=0;R[n+1]=0;
for(int i=1;i<=n;i++){
L[i]=min(L[i],a[i]);
L[i]=min(L[i],i);
L[i]=min(L[i],L[i-1]+1);
}
for(int i=n;i>=1;i--){
R[i]=min(R[i],a[i]);
R[i]=min(R[i],n-i+1);
R[i]=min(R[i],R[i+1]+1);
}
int ans=0;
for(int i=1;i<=n;i++){
ans=max(ans,min(L[i],R[i]));
}
cout<<ans<<endl;
return 0;
}