给定一个递增序列,a1 <a2 <...<an 。定义这个序列的最大间隔为d=max{ai+1 - ai }(1≤i<n),现在要从a2 ,a3 ..an-1 中删除一个元素。问剩余序列的最大间隔最小是多少?
第一行,一个正整数n(1<=n<=100),序列长度;接下来n个小于1000的正整数,表示一个递增序列。
输出答案。
5 1 2 3 7 8
4
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()){ int n = in.nextInt(),i; int arr[] = new int[n]; for (i = 0; i < n; i++) { arr[i] = in.nextInt(); } int maxFull = Integer.MIN_VALUE,minMaxGap = Integer.MAX_VALUE; for (i = 1; i < n; i++) { maxFull = Math.max(maxFull, arr[i]-arr[i-1]); } for (i = 1; i < n-1; i++) { minMaxGap = Math.min(minMaxGap, Math.max(arr[i+1]-arr[i-1], maxFull)); } System.out.println(minMaxGap); } in.close(); } }
第一思路就是O(n^2)的暴力 试了一下居然能过
题目很人性化的设计了不能删掉第一个和最后一个,省去了不少麻烦的判断。
但是此题显然有更好的办法 提供一个O(n)的解法
删去的数左右差会变成一个更大的差,和最大差比较一下即可 然后在求这个比较后较大值的最小值
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 1e2 + 5;
int arr[maxn];
int main(){
int n;
while(scanf("%d", &n) == 1){
memset(arr, 0, sizeof(arr));
for(int i=0;i<n;i++) scanf("%d", &arr[i]);
int max_a = 0, min_a = 1e5;
for(int i=1; i<n; i++) max_a = max(max_a, arr[i]-arr[i-1]);
for(int i=1; i<n-1; i++) {
min_a = min(max(max_a, arr[i+1]-arr[i-1]), min_a);
}
printf("%d\n", min_a);
}
return 0;
}
//最大间隔的最小值实际就是原来数组相邻元素间隔的最大值 import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int[] num = new int[n]; int[] d1 = new int[n - 1]; for(int i = 0; i < n; i++){ num[i] = sc.nextInt(); } for(int i = 1; i < n; i++){ d1[i - 1] = num[i] - num[i - 1]; } Arrays.sort(d1); System.out.println(d1[n - 2]); } } }
python解法如下:
gaps是两个相临数之间的差的列表
delgaps是删除一个数后,相临两个数之间的差的列表。
如果gaps中最大数大于等于delgaps中最小的数,直接返回该数。
否则返回delgaps中最小的数。
while True:
try:
a,b=input(),list(map(int,input().split()))
gaps,delGaps=[],[]
for i in range(1,len(b)):
gaps.append(b[i]-b[i-1])
for i in range(1,len(gaps)):
delGaps.append(gaps[i]+gaps[i-1])
print(max(gaps) if max(gaps)>=min(delGaps) else min(delGaps))
except:
break
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ int n=in.nextInt(); int[] a=new int[n]; for(int i=0; i<n; i++) a[i]=in.nextInt(); int max=0; for(int i=1; i<n;i++) max=Math.max(max, a[i]-a[i-1]); System.out.println(max); } } }
#include<iostream> using namespace std; int main(){ int n; while (cin >> n){ int index1 = 0; int a,b; cin >> a; int Max1 = -1; int Max2 = -1; for (int i = 0; i < n-1 ; ++i){ cin >> b; int v = b - a; if (v > Max1){ Max1 = v; index1 = i; } else if (v > Max2){ Max2 = v; } a = b; } cout << ((index1 == 0 || index1 == n - 2) ? Max2 : Max1)<<endl; } return 0; }
//正常人写法,适合新手看 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int[] a= new int[n]; for(int i = 0;i<n;i++) a[i] =scanner.nextInt(); int max_d = Integer.MIN_VALUE; for(int i=1;i<n;i++){ int d = a[i] - a[i-1]; if(d>max_d) max_d = d; } int max_min_d = max_d; boolean flag = false; for(int i=2;i<n;i++){ int d = a[i]-a[i-2]; if(d<max_d) { flag= true; break; } else if(max_min_d == 4 && d>max_min_d){ max_min_d = d; }else if(d<max_min_d){ max_min_d = d; } } if(flag) System.out.println(max_d); else System.out.println(max_min_d); } scanner.close(); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNext()) { int n = scan.nextInt(); int[] array = new int[n]; for (int i = 0; i < n; ++i) { array[i] = scan.nextInt(); } int[] dis = new int[n - 1]; int max = Integer.MIN_VALUE; for (int i = 1; i < n; ++i) { dis[i - 1] = array[i] - array[i - 1]; max = Math.max(dis[i - 1], max); } int min = Integer.MAX_VALUE; for (int i = 1; i < dis.length; ++i) { int temp = Math.max(max, dis[i] + dis[i - 1]); min = Math.min(min, temp); } System.out.println(min); } } }
//遍历一次得到max。 //len为3,max需要更新,其他情况直接输出即可。 #include <iostream> #include <vector> using namespace std; int main() { vector<int> data; int n; while(cin>>n) { data.clear(); int m; for(int i=0;i<n;i++) { cin>>m; data.push_back(m); } int max=-1; int len=data.size(); for(int i=1;i<len;i++) { if((data[i]-data[i-1])>max) { max=data[i]-data[i-1]; } } if(len==3) { return data[2]-data[0]; } else { cout<<max<<endl; } } return 0; }
n = int(input()) a = list(map(int, input().strip().split())) maxDiff, tempMax = a[n] - a[n - 1], float('inf') for i in range(1, n - 1): maxDiff = max(a[i] - a[i - 1], maxDiff) tempMax = min(a[i + 1] - a[i - 1], tempMax) print(min(maxDiff, tempMax))
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); int[] array=new int[n]; array[0]=sc.nextInt(); int[] diffArray=new int[n]; int max=0; for(int i=1;i<n;i++){ array[i]=sc.nextInt(); diffArray[i]=array[i]-array[i-1]; if(diffArray[i]>max){ max=diffArray[i]; } } int tempMax=Integer.MAX_VALUE; for(int i=1;i<n-1;i++){ if((diffArray[i]+diffArray[i+1])<max){ tempMax=max; break; }else{ if(tempMax>(diffArray[i]+diffArray[i+1])) tempMax=(diffArray[i]+diffArray[i+1]); } } System.out.println(tempMax); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); int[] a=new int[n]; int[] k=new int[n-1]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } for(int i=0;i<n-1;i++){ k[i]=a[i+1]-a[i]; } int d=getMax(k); int[] mind=new int[n-2]; for(int i=0;i<n-2;i++){ if(k[i]+k[i+1]<d){ mind[i]=d; }else{ mind[i]=k[i]+k[i+1]; } } int mindis=getMin(mind); System.out.println(mindis); } } public static int getMax(int[] arr) { int max = arr[0]; for(int i=0;i<arr.length;i++) { if(arr[i]>max) max = arr[i]; } return max; } public static int getMin(int[] arr) { int min = arr[0]; for(int i=0;i<arr.length;i++) { if(arr[i]<min) min = arr[i]; } return min; } }
模拟整个求值的过程,然后找出最小值就可以了! #include<iostream> #include<vector> using namespace std; int main() { int n; while(cin>>n) { vector<int> input(n); for(int i=0;i<n;i++) cin>>input[i]; vector<int> small; int max; for(int i=1;i<n-1;i++) { max=-1000000; for(int j=1;j<n;j++) { int k=j-1; if(j==i) j++; if(input[j]-input[k]>max) max=input[j]-input[k]; } small.push_back(max); } max=small[0]; for(int i=0;i<small.size();i++) { if(small[i]<max) max=small[i]; } cout<<max<<endl; } return 0; }
import java.util.ArrayList; import java.util.Scanner; //笨人只能想出笨方法 public class Mogu2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int n = scanner.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } System.out.println(minOfMaxReDis(n, a)); } } public static int minOfMaxReDis(int n, int[] a) { ArrayList<Integer> list = new ArrayList<Integer>(); for(int i=0;i<n;i++){ list.add(a[i]); } int minReDis = Integer.MAX_VALUE; for(int i=1;i<=n-2;i++){ int removeNum = list.remove(i); int mintemp= maxDis(list); if(mintemp < minReDis){ minReDis = mintemp; } list.add(i, removeNum); } return minReDis; } public static int maxDis(ArrayList<Integer> list) { int maxDis = 0; for(int i=1;i<list.size();i++){ int diff = list.get(i) - list.get(i-1); if(diff > maxDis){ maxDis = diff; } } return maxDis; } }
public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } process(arr, n); } in.close(); } private static void process(int[] arr, int n) { // TODO Auto-generated method stub int maxInterval=-1;//记录原序列间隔的最大值 int minNewInterval=Integer.MAX_VALUE;//记录新的最大间隔 int [] dp=new int[n-1];//记录相邻两个元素的间隔 for(int i=1;i<=n-1;i++) { dp[i-1]=arr[i]-arr[i-1]; if (dp[i-1]>maxInterval) { maxInterval=dp[i-1]; } } //原序列的间隔进行合并相当于删除原素,当大于maxInterval的时候选取最小值 for (int i = 0; i < dp.length-1; i++) { int merge=dp[i]+dp[i+1]; if (merge>maxInterval) { minNewInterval=Math.min(minNewInterval, merge); } else { minNewInterval=Math.min(minNewInterval, maxInterval); } } System.out.println(minNewInterval); } }
/* 循环删掉数组中的一个元素,将新数组存在arr1中,再将其赋给temp数组。算出temp数组中每个差值 求出最大的差值,放在value数组中,最后在value数组中找出最小的值输出。 */ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()){ int n = in.nextInt(); int[] arr = new int[n]; for(int i=0;i<arr.length;i++){ arr[i] = in.nextInt(); } System.out.println(getValue(arr)); } } public static int getValue(int[] arr){ int result = 100; //删除后的间隔数组 int[] value = new int[arr.length-2]; int[] temp = new int[arr.length-1]; for(int i=1;i<arr.length-1;i++){ int[] arr1 = new int[arr.length-1]; for(int m=0;m<i;m++){ arr1[m] = arr[m]; } for(int j=i+1;j<arr.length;j++){ arr1[j-1] = arr[j]; } for(int k=0;k<temp.length;k++){ temp[k] = arr1[k]; } int max = 0; for(int l=1;l<temp.length;l++){ int cha = temp[l] - temp[l-1]; if(cha > max){ max = cha; } } value[i-1] = max; } for(int i = 0;i<value.length;i++){ if(value[i] < result){ result = value[i]; } } return result; } }
#include<stdio.h> #include<iostream> #include<vector> #include<limits.h> using namespace std; int main(){ int N; while(cin>>N){ vector<int> Num(N,0); int MaxGap = INT_MIN; int ret = INT_MAX; for(int i=0; i<N; ++i){ cin >> Num[i]; } for(int i=0; i<N-1; ++i){ MaxGap = max(MaxGap,Num[i+1]-Num[i]); } for(int i=1; i<=N-2; ++i){//最大值只会出现在MaxGap和a[i+1]-a[i-1]中 int temp = max(Num[i+1]-Num[i-1],MaxGap);//消除i的结果 ret = min(ret,temp);//最小的最大间隔 } cout << ret <<endl; } return 0; }
#include <iostream> using namespace std; int Count(int* p,int len){ int max=0; for(int i=1;i<len;i++) if(max<p[i]-p[i-1]){ max=p[i]-p[i-1]; } return max; } int min(int a, int b){ return a>b?b:a; } int main(){ int N; while(cin>>N){ int *p=new int[N]; for(int i=0;i<N;i++) cin>>p[i]; cout<<min(min(Count(p,N),Count(p+1,N-1)),Count(p,N-1))<<endl; } return 0; }