Codeforces Round #283 (Div. 2) A

來源:互聯網
上載者:User

標籤:des   blog   ar   io   os   sp   for   on   div   

解題思路:給出一個遞增數列,a1,a2,a3,-----,an.問任意去掉a2到a3之間任意一個數之後,

因為注意到該數列是單調遞增的,所以可以先求出原數列相鄰兩項的差值的最大值max,

得到新的一個數列(比如先去掉a2),該數列相鄰兩項的差值的最大值為Max1=findmax(max,a3-a1)

再去掉 a3,得到該數列相鄰兩項的差值的最大值Max2=findmax(max,a4-a2)

------

再去掉 an-1,得到該數列相鄰兩項的差值的最大值Maxn-2=findmax(max,an-an-2)

題目要求的即為 max1,max2,max3,-----,maxn-2的最小值

 

A.

Minimum Difficulty

time limit per test2 seconds

memory limit per test256

megabytes inputstandard input outputstandard 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 ai increase, 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.

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

 

#include<stdio.h>int searchmax(int a[],int n){int i,max;max=a[1];for(i=2;i<=n;i++){if(a[i]>max)max=a[i];}return max;}int searchmin(int a[],int n){int i,min;min=a[1];for(i=2;i<=n;i++){if(a[i]<min)min=a[i];}return min;}int findmax(int a,int b){if(a>b)return a;elsereturn b;}int main(){int a[105],d[105],e[105],i,n,s,j,k,max;while(scanf("%d",&n)!=EOF){k=1;max=0;for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<n;i++)d[k++]=a[i+1]-a[i];max=searchmax(d,n-1);j=1;for(i=2;i<=n-1;i++){e[j++]=findmax(max,a[i+1]-a[i-1]);//找到刪掉一個數後的數列中,相鄰兩項的差值的最大值     }printf("%d\n",searchmin(e,n-2));}}

  

Codeforces Round #283 (Div. 2) A

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.