首页 > 试题广场 >

两两配对差值最小

[编程题]两两配对差值最小
  • 热度指数:7878 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定一个长度为偶数的数组arr,将该数组中的数字两两配对并求和,在这些和中选出最大和最小值,请问该如何两两配对,才能让最大值和最小值的差值最小?

输入描述:
一共2行输入。
第一行为一个整数n,2<=n<=10000, 第二行为n个数,,组成arr数组,0<=arr[i]<=100。


输出描述:
输出最小的差值。
示例1

输入

4
2 6 4 3

输出

1
示例2

输入

6
11 4 3 5 7 1

输出

3
贪心是不错,就是不太好证明。只能用“平均”的思想浅浅理解了。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Collections;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            List<Integer> arr = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                arr.add(in.nextInt());
            }
            Collections.sort(arr);
            //
            int mi = 200 + 5;
            int mx = -1;
            int i = 0, j = n - 1;
            while (i < j) {
                int k = arr.get(j) + arr.get(i);
                mi = Math.min(mi, k);
                mx = Math.max(mx, k);
                i++;
                j--;
            }
            System.out.print(mx - mi);
        }
    }
}

发表于 2023-11-29 17:09:18 回复(0)
排序+左右指针
import java.util.Scanner;
import java.util.Arrays;
import java.lang.Math;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] nums = new int[n];
        for(int i=0;i<n;i++){
            nums[i] = in.nextInt();
        }
        Arrays.sort(nums);
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        int left = 0;
        int right = n-1;
        while(left<right){
            int sum = nums[left]+nums[right];
            max = Math.max(max,sum);
            min = Math.min(min,sum);
            left++;
            right--;
        }
        System.out.println(max-min);
    }
}


发表于 2021-08-24 10:07:48 回复(0)
看前几位的做法都是先排序,然后前后匹配,再排序,最后输出最大差
可是如果数量太多的话,比如这个题给到了10000,这个排序会比较耗时吧?所以可以简化一下这次排序,核心思路不变
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        int N=Integer.parseInt(bf.readLine());
        List<Integer> list=new LinkedList<>();
        String[] sarr=bf.readLine().split(" ");
        int[] arr=new int[101];
        for(String s:sarr)
            arr[Integer.parseInt(s)]++;
        for(int start=0,end=100,n=N/2;n>0;){
            while(arr[start]==0)
                start++;
            while(arr[end]==0)
                end--;
            list.add(start+end);
            if(arr[start]<arr[end]){
                n-=arr[start];
                arr[end]-=arr[start];
                arr[start]=0;
            }else{
                n-=arr[end];
                arr[start]-=arr[end];
                arr[end]=0;
            }
        }
        Collections.sort(list);
        System.out.println(list.get(list.size()-1)-list.get(0));
    }
}
发表于 2019-07-14 16:56:06 回复(0)