In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.
In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after a i days of rain (on day a i tile isn’t destroyed yet, and on day a i + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.
The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
Input
The first line contains integer n (1 ≤ n ≤ 103) — the boulevard’s length in tiles.
The second line contains n space-separated integers a i — the number of days after which the i-th tile gets destroyed (1 ≤ a i ≤ 103).
Output
Print a single number — the sought number of days.
Examples
inputCopy
4
10 3 5 10
outputCopy
5
inputCopy
5
10 2 8 3 5
outputCopy
5
Note
In the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can’t jump over it.
In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
题意
给你一个n,表示有n块瓷砖,你需要从第n块瓷砖移动到第1块瓷砖,一步可以走1块瓷砖或2块瓷砖。现在有一场雨,瓷砖的质量差,在雨中会受到破坏(第i块瓷砖在第a[i]+1天时会被销毁),需要你求出最晚在哪一天还能从第n块走到第1块。
思路
从n能到1,那么从1也能到n,我们不需要倒过来处理数组
这题考查dp,我们用dp[i]表示能跳到第i块瓷砖最迟的那一天
所以dp[n]就是所求的解
状态转移方程:dp[i] = min(max(dp[i - 1], dp[i - 2]), a[i]);
解释一下
要想跳到i,肯定要在第i块瓷砖销毁前
第i块瓷砖最晚的销毁时间是a[i],
如果已经被销毁了,dp[i]就取a[i]
如果还没销毁,如果前两块都跳不上(第i块是从第i-1块或第i-2块跳过来的,只要有一块能跳过来就行,所以取max),就取max(dp[i - 1], dp[i - 2])
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
//#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int a[1010];
int dp[1010];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
dp[1] = a[1];
for (int i = 2; i <= n; i++)
{
dp[i] = min(max(dp[i - 1], dp[i - 2]), a[i]);
}
cout << dp[n] << endl;
}