573B - Bear and Blocks
2015-09-01 17:23
316 查看
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.
Sample test(s)
input
output
input
output
Note
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.
题意:给如图的方块列,每次刷新去除包围最外面的一层,问多少次去除全部方块。
思路:模拟必然超时。
可以发现,第一次去除后,第i个的高度hi = min(hi - 1, hi - 1, hi + 1)
第二次去除后,第i个的高度 hi = max(0, min(hi - 2, hi - 1 - 1, hi - 2, hi + 1 - 1, hi + 2))
由此,第k次去除后第i个的高度为hi = min(Left, Right) where Left = min(hi - j - (k - j)) = min(hi - j + j - k)
所以当 k = min(hi - j + j)时,Left变为0;同理可求得right为0的最小k值;
最后取最大的k便可。
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.
Sample test(s)
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.
![](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.
题意:给如图的方块列,每次刷新去除包围最外面的一层,问多少次去除全部方块。
思路:模拟必然超时。
可以发现,第一次去除后,第i个的高度hi = min(hi - 1, hi - 1, hi + 1)
第二次去除后,第i个的高度 hi = max(0, min(hi - 2, hi - 1 - 1, hi - 2, hi + 1 - 1, hi + 2))
由此,第k次去除后第i个的高度为hi = min(Left, Right) where Left = min(hi - j - (k - j)) = min(hi - j + j - k)
所以当 k = min(hi - j + j)时,Left变为0;同理可求得right为0的最小k值;
最后取最大的k便可。
#include<bits/stdc++.h> using namespace std; const int nax = 1e6 + 5; const int inf = 1e9 + 5; int t[nax], res[nax]; int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d", &t[i]); int worst = 0; for(int i = 1; i <= n; ++i) { worst = min(worst, t[i]-i);// 求min(hi-j - j)=min(hi-j -(i-j)+i) res[i] = i + worst; } worst = n + 1; for(int i = n; i >= 1; --i) { worst = min(worst, t[i]+i);//同理求min(hi+j + j-i) res[i] = min(res[i], worst-i); } int R = 0; for(int i = 1; i <= n; ++i) R = max(R, res[i]); printf("%d\n", R); return 0; }
相关文章推荐
- 透视投影详解
- centos安装mediawiki
- datagrid 给符合条件的数据设置样式
- 运算符“异或”^ 的使用
- 项目开发中的心得·
- python 遇到的异常
- 浅析浮点数与定点数
- eclipse的egit插件问题
- JS操作Button、Select和Radio
- 010Editor的ELFTemplate 16进制改造
- Android Spinner 的具体用法
- IOS学习笔记 ---- 15/09/01
- Floyd算法
- 感知开源的力量-APICloud Studio开源技术分享会
- 在你步入职业软件开发生涯那天就该知道的5件事
- Docker实践
- 脑子发热的时候别说话
- Linux系统下设置Oracle开机自动启动
- WebSphere是什么?
- 快速排序的几个实现及其在效率上的考虑