【CF】【318div2D】【思维题】【DP|数学】
2015-08-30 11:10
381 查看
D. 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
题解的方法 就是直接算出了答案的公式。。。 hi = min(Left, Right) where Left = min(hi - j - (k - j)) = min(hi - j + j - k) for
![](http://codeforces.com/predownloaded/3a/89/3a895926c4fa4fd80dd1e0ac8cf10dbd86c86b3b.png)
然后求这个的时候 用了o(n)的小技巧。
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
题解的方法 就是直接算出了答案的公式。。。 hi = min(Left, Right) where Left = min(hi - j - (k - j)) = min(hi - j + j - k) for
![](http://codeforces.com/predownloaded/3a/89/3a895926c4fa4fd80dd1e0ac8cf10dbd86c86b3b.png)
然后求这个的时候 用了o(n)的小技巧。
#include <iostream> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <list> #include <map> #include <set> #include <string> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define mp push_back int n; int arr[100010]; int ret[100010]; int main() { //freopen("in.txt","r",stdin); while(scanf("%d",&n) != EOF) { for(int i=1;i<=n;i++) { scanf("%d",&arr[i]); } int low = 0; for(int i=1;i<=n;i++) { low = min(low,arr[i]-i); ret[i] = low + i; } low = n + 1; for(int i=n;i>=1;i--) { low = min(low,arr[i]+i); ret[i] = min(ret[i],low - i); } int ans = 0; for(int i=1;i<=n;i++) { ans = max(ans,ret[i]); } printf("%d\n",ans); } return 0; }
相关文章推荐