Codeforces Round #283 (Div. 2)---A. Minimum Difficulty (暴力)
来源:互联网 发布:新纪元软件官网 编辑:程序博客网 时间:2024/06/06 17:06
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.
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).
Print a single number — the minimum difficulty of the track after removing a single hold.
31 4 6
5
51 2 3 4 5
2
51 2 3 7 8
4
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.
题意:给一列n个数,让你选出删除一个中间值(第一个和最后一个不可删)之后,相邻两数之间的差值的最大值,问这个最大值最小可以是多少。
分析:暴力可解。枚举删除值的位置,每次求出间距的最大值即可。
AC代码:
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define INF 0x7fffffffint a[105];int main(){ #ifdef sxk freopen("in.txt","r",stdin); #endif int n; while(scanf("%d",&n)!=EOF) { for(int i=0; i<n; i++) scanf("%d", &a[i]); int ans = 123456789; for(int i=1; i<n-1; i++){ //枚举删除元素的位置,注意下标1~n-2 int xx = 0; for(int j=1; j<n; j++){ int x; if(j == i){ //删除的位置,直接越过 j ++; x = a[j] - a[j-2]; } else{ x = a[j] - a[j-1]; } if(xx < x) xx = x; } if(ans > xx) ans = xx; } printf("%d\n", ans); } return 0;}
- Codeforces Round #283 (Div. 2)---A. Minimum Difficulty (暴力)
- Codeforces 496A Minimum Difficulty(暴力)
- Codeforces Round #283 (Div. 2) A. Minimum Difficulty B. Secret Combination C. Removing Columns
- Codeforces Round #271 (Div. 2) --A Keyboard (暴力)
- Codeforces Round #281 (Div. 2)A(构造+暴力模拟)
- Codeforces Round #344 (Div. 2) A 题题解 (暴力)
- Codeforces Round #354 (Div. 2)(A)暴力,模拟
- Codeforces Round #433 (Div. 2) A.Fraction(暴力)
- Codeforces 496A. Minimum Difficulty
- Codeforces Round #283 (Div. 2)---B. Secret Combination (暴力)
- Codeforces Round #329 (Div. 2) A. 2Char 字符串+暴力
- Codeforces Round #243 (Div. 2) A,B,C(暴力枚举)
- Codeforces Round #286 (Div. 2) A.(回文串暴力)
- Codeforces Round #387(Div. 2)A. Display Size【暴力】
- Codeforces Round #396 (Div. 2) A---C 暴力+dp
- Codeforces Round #447 (Div. 2) A. QAQ 暴力
- Codeforces Round #284 (Div. 2)---A. Watching a movie (暴力 + 贪心)
- Codeforces Round #329 (Div. 2)A. 2Char(暴力枚举)
- 在linux中,如何增加、修改、删除、暂停和冻结用户名
- k-means算法代码
- windows下 远程DLL注入
- Windows 更新错误 8024401C -解决方法之一
- linux文件系统启动流程 ---笔记整理
- Codeforces Round #283 (Div. 2)---A. Minimum Difficulty (暴力)
- C#中线程状态的判别
- Gradle实现的两种简单的多渠道打包方法
- linux下vi命令大全
- 身为程序员的我们......
- 梁启超为何善变?
- vs silverlight调试闪退
- TCP三次握手与半开连接数
- Linux开机启动过程分析