码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #283 (Div. 2) 题解

时间:2015-04-10 21:55:23      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

 
           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 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.

Sample test(s)
input
3
1 4 6
output
5
input
5
1 2 3 4 5
output
2
input
5
1 2 3 7 8
output
4


意思:一个非递增的数组,定义这个数组的难度为技术分享
   现在要从这个数组中拿掉一个数(必须拿掉),问拿掉哪个数使得这个数组的难度最小。


思路:记录这个数组(数组本身不用记录)两两的距离,然后找出a[i]+a[i+1]的最小值,就是去掉中间这个数了,再用这个a[i]+a[i+1]和数组原来的难度相比,大的那个就是ans了。

技术分享
 1 #include<cstdio>
 2 const int maxn=104;
 3 int a[maxn];
 4 int main()
 5 {
 6     int n;
 7     while(scanf("%d",&n)!=EOF)
 8     {
 9         int pre,p;
10         int tot=1;
11         scanf("%d",&pre);
12         for(int i=2;i<=n;i++)
13         {
14             scanf("%d",&p);
15             a[tot++]=p-pre;
16             pre=p;
17         }
18         int ans=100000;
19         for(int i=1;i<tot-1;i++)
20         {
21             if(a[i]+a[i+1]<ans)
22                 ans=a[i]+a[i+1];
23         }
24         for(int i=1;i<tot;i++)
25         {
26             if(a[i]>ans)
27                 ans=a[i];
28         }
29         printf("%d\n",ans);
30     }
31     return 0;
32 }
15ms

 




Codeforces Round #283 (Div. 2) 题解

标签:

原文地址:http://www.cnblogs.com/-maybe/p/4415584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!