请使用java实现下列功能:假设学校和所有的学生家,在一条直线上,请问,学校要建在什么位置,能使得学校到各个学生家的距离之和最短?输入的第一行是一个整数N(1<=n<=1000)表示有N户家庭。输入的第二行是一个数组n(i)l,n(i)i的长度(0<=n(i)<=10000),表示每个家庭的位置,所有家庭的位置都不相同。例如:输入[5,0,20,40,10,30]输出20;输入[2,0,20]输出0;输入[1,10]输出10 import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] nlist = new int[n]; for (int i = 0; i < n; i++) { nlist[i] = sc.nextInt(); } sc.close(); Arrays.sort(nlist); int left = 0, right = n - 1; int mid = left + (right - left) / 2; if(mid <= 1){ System.out.println(nlist[0]); return; } while (left <= right) { int distance1 = getDistance(mid - 1, nlist); int distance2 = getDistance(mid, nlist); int distance3 = getDistance(mid + 1, nlist); if (distance2 <= distance1 && distance2 <= distance3) { break; } else if (distance1 < distance3) { right = mid - 1; } else { left = mid + 1; } mid = left + (right - left) / 2; } System.out.println(nlist[mid]); } public static int getDistance(int x, int[] nlist) { int distance = 0; for (int i = 0; i < nlist.length; i++) { distance += Math.abs(nlist[i] - nlist[x]); } return distance; } }