校内选拔I题题解 构造题 Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) ——D
http://codeforces.com/contest/574/problem/D
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.
Print the number of operations needed to destroy all towers.
6
2 1 4 6 2 2
3
7
3 3 3 1 3 3 3
2
The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color.
![](http://codeforces.com/predownloaded/e1/2d/e12d49eda68c451a1ddfec77a1747676b180c4c5.png)
After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation
题目大意 每次只能消最外层的砖 问多少次能消完
看hint图吧 等价与从右方看 从左到右峰依次为 2 1 4 3 2 1 从左方看 从左到右峰依次为 1 1 2 3 2 2
比较每个位置需要消去的最少次数 依次为 1 1 2 3 2 2的峰
看到这里是不是就明白了呢
我们只需要把山峰等效为 突起 如 1 2 2 3 4 3这样的形式就ok了
具体操作见代码 tw菊苣的dp写法还不是很理解 再研究一下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; inline void ri(int &num){ num=0;int f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar(); num*=f; } const int N=1e5+5; int l[N],r[N],a[N]; int main() { int n; ri(n); for(int i=1;i<=n;i++)ri(a[i]); for(int i=1;i<=n;i++) l[i]=min(l[i-1]+1,a[i]); for(int i=n;i>=1;i--) r[i]=min(r[i+1]+1,a[i]); int mx=-1; for(int i=1;i<=n;i++) mx=max(mx,min(l[i],r[i])); printf("%d\n",mx); return 0; }