Codeforces 574D Bear and Blocks【思维】
来源:互联网 发布:疯狂java讲义第5版 编辑:程序博客网 时间:2024/05/17 01:23
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He builtn 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 integersh1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers.
Print the number of operations needed to destroy all towers.
62 1 4 6 2 2
3
73 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.
题目大意:
给你一个长度为N的一串序列,ai表示在这个位子上有一座高度为ai的木块。
每次操作需要去掉所有这样的木块:
一个木块如果上下左右有一个空白位子。
问一共需要操作几次,使得整个序列变成空的。
思路(思路源自:http://blog.csdn.net/nike0good/article/details/48103631):
找最长的1 2 3 4 3 2 1这样的序列。
因为我们每一轮操作一定会使得一整个序列的最左边和最右边的两个序列直接变成0。
所以我们无论是很大的数还是很小的数,我们只要找到类似1 2 3 4 3 2 1这样的序列即可。
那么O(n)维护一个数组l【i】,表示以i为山峰顶端点的话,其左边能够撑住多少次操作。
同理O(n)维护一个数组r【i】,表示以i为山峰顶端点的话,其右边能够撑住多少次操作。
那么最终O(n)枚举这个山峰顶点,其以i为山峰顶点的话,其能够撑住的操作次数为:min(l【i】,r【i】);
那么过程维护一个最大值即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int a[105000];int l[105000];int r[105000];int main(){ int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++)scanf("%d",&a[i]); a[0]=0;a[n+1]=0; l[0]=0;l[n+1]=0; r[0]=0;r[n+1]=0; 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 output=0; for(int i=1;i<=n;i++) { output=max(output,min(l[i],r[i])); } printf("%d\n",output); }}
- Codeforces 574D Bear and Blocks【思维】
- codeforces 574D. Bear and Blocks
- 【32.89%】【codeforces 574D】Bear and Blocks
- Bear and Blocks CF-574D(类似dp+思维)
- Codeforces Round #318 574D Bear and Blocks(dp)
- Codeforces Round #318-(D. Bear and Blocks)
- Codeforces Round #318 D. Bear and Blocks
- Codeforces Round #318 D. Bear and Blocks
- Codeforces 318 div2.D Bear and Blocks(dp,贪心)
- Codeforces Round #318 (Div. 2) D - Bear and Blocks
- Codeforces Round #318 D. Bear and Blocks DP
- Codeforces 574C Bear and Poker【思维】
- CodeForces #318 (div1) B.Bear and Blocks
- codeforces 573B Bear and Blocks
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)D Bear and Blocks
- Codeforces 574B Bear and Three Musketeers【思维】
- Codeforces 639C Bear and Polynomials 【思维】
- CodeForces 385D Bear and Floodlight
- Android_Lottery_Location
- 书上的题目L-12
- 1003. Emergency(最短路)
- VirtualBox网络配置
- Mac studio查看sha1
- Codeforces 574D Bear and Blocks【思维】
- memcache session共享
- iOS——UITableViewCell
- 来到csdn的第一天,先记录一个谷歌网页打不开的解决方式吧!
- T extends Comparable<T>
- 1+2+3....+n的构造函数求法(类的静态成员变量)
- Ubuntu ,anaconda安装tensorflow最新GPU-1.0.0版本
- NYOJ 211 tree(二叉树的遍历与创建)
- JavaScript中的this详解(1)