• Bear and Blocks (dp+思维)


    Bear and Blocks

     CodeForces - 574D

    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.

    题意:给你一些由小方块组成的柱子,每次把裸露在外面的小方块即周围有白色方块的小方块消掉,问把这些方块全部消掉需要操作多少次。

    思路:先从左到右维护每一个柱子完全消除的最小时间,再从右往左维护一遍,最后取两者的最大值即该柱子被完全消除的时间。

    以从左往右为例,一个柱子被完全消除由两个因素决定,即和它相邻的左边的柱子完全需要的时间和该柱子高度。比如说1 4 对于4这个柱子来说,第一次操作消除1和4上面的3个,第二次操作将第二个柱子完全消除,所以l[2]=min(l[1]+1,a[2]);因为对于一个柱子来说,如果它的左侧一直消除不完,或者它的顶部一直消除不完,操作就要一直持续下去。

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<iostream>
     5 using namespace std;
     6 const int maxn=1e5+10;
     7 int a[maxn];
     8 int l[maxn];
     9 int r[maxn];
    10 int ans=0;
    11 int n;
    12 int main()
    13 {
    14     cin>>n;
    15     for(int i=1;i<=n;i++)
    16     {
    17         scanf("%d",&a[i]);
    18     }
    19     l[0]=0;
    20     r[n+1]=0;
    21     for(int i=1;i<=n;i++)
    22     {
    23         l[i]=min(l[i-1]+1,a[i]);
    24     }
    25     for(int i=n;i>=1;i--)
    26     {
    27         r[i]=min(r[i+1]+1,a[i]);
    28     }
    29     for(int i=1;i<=n;i++)
    30     {
    31         ans=max(ans,min(l[i],r[i]));
    32     }
    33     cout<<ans<<endl;
    34 }
  • 相关阅读:
    windows下安装redis
    关于 tomcat 集群中 session 共享的三种方法
    利用Nginx做负载均衡
    Spring4 MVC Hibernate4集成
    <mvc:annotation-driven />到底帮我们做了啥
    Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别
    JPA注解指南
    Hibernate与Jpa的关系
    ActiveMQ实现负载均衡+高可用部署方案
    信号接口-视频输出端口介绍
  • 原文地址:https://www.cnblogs.com/1013star/p/9700490.html
Copyright © 2020-2023  润新知