Codeforces Round #283 (Div. 2) A. Minimum Difficulty【一个数组定义困难值是两个相邻元素之间差的最大值。 给一个数组,可以去掉任意一个元素,问剩余数列的困难值的最小值是多少】

A. Minimum Difficulty

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Mike is trying rock climbing but he is awful at it.

There are n holds on the wall, i-th hold is at height ai off the ground. Besides, let the sequence aiincrease, that is, ai < ai + 1 for all i from 1 to n - 1; we will call such sequence a track. Mike thinks that the track a1, ..., an has difficulty . In other words, difficulty equals the maximum distance between two holds that are adjacent in height.

Today Mike decided to cover the track with holds hanging on heights a1, ..., an. To make the problem harder, Mike decided to remove one hold, that is, remove one element of the sequence (for example, if we take the sequence (1, 2, 3, 4, 5) and remove the third element from it, we obtain the sequence(1, 2, 4, 5)). However, as Mike is awful at climbing, he wants the final difficulty (i.e. the maximum difference of heights between adjacent holds after removing the hold) to be as small as possible among all possible options of removing a hold. The first and last holds must stay at their positions.

Help Mike determine the minimum difficulty of the track after removing one hold.

Input

The first line contains a single integer n (3 ≤ n ≤ 100) — the number of holds.

The next line contains n space-separated integers ai (1 ≤ ai ≤ 1000), where ai is the height where the hold number i hangs. The sequence ai is increasing (i.e. each element except for the first one is strictly larger than the previous one).

Output

Print a single number — the minimum difficulty of the track after removing a single hold.

Examples

input

31 4 6

output

5

input

51 2 3 4 5

output

2

input

51 2 3 7 8

output

4

Note

In the first sample you can remove only the second hold, then the sequence looks like (1, 6), the maximum difference of the neighboring elements equals 5.

In the second test after removing every hold the difficulty equals 2.

In the third test you can obtain sequences (1, 3, 7, 8), (1, 2, 7, 8), (1, 2, 3, 8), for which the difficulty is 4, 5 and 5, respectively. Thus, after removing the second element we obtain the optimal answer — 4.

【题意】:对一个数组,定义困难值是两个相邻元素之间差的最大值。
现在又一个数组,可以去掉任意一个元素,问剩余数列的困难值的最小值是多少。

【分析】:由于数据小,可以枚举去掉的元素然后暴力更新,复杂度是 O(nn)。如果数据比较大的话,可以利用动态规划O(n)过。

【代码】:

#include <bits/stdc++.h>
#define N 100010
using namespace std;
#define oo 99999
int a[110];
int b[110];

int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }

    int ans=oo;
    for(int i=2;i<n;i++){
        int k=0;
        for(int j=1;j<=n;j++){
            if(j==i)continue;
            b[k++]=a[j];
        }
        int MAX=0;
        for(int j=0;j<n-1;j++){
            MAX=max(MAX,b[j+1]-b[j]);
        }
        ans=min(ans,MAX);
    }
    cout<<ans<<endl;
    return 0;
}

  

时间: 2024-10-12 16:44:27

Codeforces Round #283 (Div. 2) A. Minimum Difficulty【一个数组定义困难值是两个相邻元素之间差的最大值。 给一个数组,可以去掉任意一个元素,问剩余数列的困难值的最小值是多少】的相关文章

Codeforces Round #283 (Div. 2) a

/**  * @brief Codeforces Round #283 (Div. 2) a  * @file a.cpp  * @author mianma  * @created 2014/12/18 17:45  * @edited  2014/12/18 17:45  * @type brute  * @note  */ #include <fstream> #include <iostream> #include <cstring> using namespa

构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination

题目传送门 1 /* 2 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-3 8:43:02 7 File Name :A.cpp 8 *************************************************/ 9 1

暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns

题目传送门 1 /* 2 题意:删除若干行,使得n行字符串成递增排序 3 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-3 10:49:53 8 File Name :C.cpp 9 ************************************

Codeforces Round #283 (Div. 2) c

/**  * @brief Codeforces Round #283 (Div. 2) c  * @file c.cpp  * @author mianma  * @created 2014/12/22 13:38  * @edited  2014/12/22 13:38  * @type greedy  * @time cost time O(m*n)  * @mem  cost mem  2*MAXN^2 + 2*MAXN  * @note  */ #include <fstream>

Codeforces Round #283 (Div. 2) b

/**  * @brief Codeforces Round #283 (Div. 2) b  * @file b.cpp  * @author mianma  * @created 2014/12/19 10:50  * @edited  2014/12/19 10:50  * @type brute  * @note  */ #include <fstream> #include <iostream> #include <cstring> using namespa

CodeForces Round #283 Div.2

A. Minimum Difficulty 题意: 有一个递增序列a,现在要去掉除了第一项和最后一项元素外的某一项,使新数列中相邻元素之差的最大值最小. 分析: 先求出原序列a中,相邻两项元素之差的最大值m. 枚举去掉ai,则新序列相邻元素差的最大值为max{ m, ai+1 - ai },然后记录最小值即可. 1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int maxn =

Codeforces Round #283 (Div. 2) 题解

A. Minimum Difficulty Mike is trying rock climbing but he is awful at it. There are n holds on the wall, i-th hold is at height ai off the ground. Besides, let the sequence ai increase, that is, ai < ai + 1 for all ifrom 1 to n - 1; we will call such

Codeforces Round #283 (Div. 1)解题报告A.B.C.

A - Removing Columns 贪心. 只能是竖着不递减的就尽量选上,当某一行出现字典序大于上一行的情况的时候,就不用再考虑这一行. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #inc

Codeforces Round #411 (Div. 2)D. Minimum number of steps(贪心)

传送门 Description We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a subs