首先排序

左右指针
这两种情况可以合并
array[r] == length || array[r]+array[l] > length

array[r]+array[l] <= length

关键 每个最多放两个 因此不用考虑太多 不用考虑 再放第三个

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
         Scanner in = new Scanner(System.in);
        int length = in.nextInt();
        ArrayList<Integer> items = new ArrayList<Integer>();
        while(in.hasNext()){
            items.add(in.nextInt());
        }
        items.sort(Comparator.naturalOrder());
        Integer[] array = items.toArray(new Integer[0]);

        int l = 0;
        int r = items.size()-1;

        int res = 0;
        while(l<=r){
            if(array[r] == length || array[r]+array[l] > length){
                res++; r--;
            }
            else if(array[r]+array[l] <= length ){
                res++; r--; l++;
            }
        }
        System.out.println(res);
    }
}