zoukankan      html  css  js  c++  java
  • codeforces 574D. 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.

    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.

    After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.


    刚用模拟做T了,后来发现可以用简单的dp做。记录l[i]为从左到右消去这一行的最小操作数,r[i]为从右到左消去这一行的最小操作数,那么l[i]=r[n]=1.

    l[i]=min(h[i],l[i-1]+1),r[i]=min(h[i],r[i+1]+1),然后只要求出最大操作数t[i]=max(l[i],r[i])的最小值就行了。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define ll long long
    #define inf 0x7fffffff
    #define maxn 100060
    int l[maxn],r[maxn],h[maxn],t[maxn];
    
    int main()
    {
        int n,m,i,j,ans;
        while(scanf("%d",&n)!=EOF)
        {
            for(i=1;i<=n;i++){
                scanf("%d",&h[i]);
            }
            l[1]=1;r[n]=1;
            for(i=2;i<=n;i++){
                l[i]=min(h[i],l[i-1]+1);
    
            }
            for(i=n-1;i>=1;i--){
                r[i]=min(h[i],r[i+1]+1);
            }
            ans=0;
            for(i=1;i<=n;i++){
                t[i]=min(l[i],r[i]);
                ans=max(t[i],ans);
            }
            printf("%d
    ",ans);
    
        }
        return 0;
    }
    


  • 相关阅读:
    fatfs输出目录
    《基于多光程长的高散射物质光学参数的测量及其应用》论文
    《2013李永乐线性代数强化班》视频1,2,3,4
    oled屏幕模块
    python中数据结构
    大数据python词频统计之hdfs分发-cacheFile
    8大排序之Python实现 冒泡排序优化
    大数据python词频统计之本地分发-file
    2019-04-30vmware虚拟机安装macos 10.8格式为iso
    2019-04-24Scurecrt 如何下载文本文件
  • 原文地址:https://www.cnblogs.com/herumw/p/9464660.html
Copyright © 2011-2022 走看看