有N个人想要过一条河,但是他们只有一条最多载两人的船。因此必须想出一个调度船来回的方法让每个人都能过河。每个人都有自己的划船速度,且同一条船上的两个人取决于慢者的速度。你的任务就是想出一个每人都能过河的最快策略。
输入格式:
输入的第一行是一个正整数T(1 <= T <= 20),表示测试用例的组数。下面是T组用例。每个用例的第一行是正整数N,第二行是N个正整数表示每个人的划船速度。每组用例不会超出1000个人,每个人的划船时间不会超过100秒。
输出格式:
对于每个用例,输出所有N个人都能过河的最短时间(秒)。
输入样例:
2
3
1 3 7
4
1 2 5 10
输出样例:
11
17
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int k = sc.nextInt();
int Boat[] = new int[k];
for (int j = 0; j < Boat.length; j++) {
Boat[j] = sc.nextInt();
}
Arrays.sort(Boat);
if (Boat.length == 3) {
System.out.println(Boat[0] + Boat[1] + Boat[2]);
} else if (Boat.length == 2) {
System.out.println(Boat[1]);
} else if (Boat.length == 1) {
System.out.println(Boat[0]);
}else {
int sum=0;
for (int p= Boat.length-1;p>=0;p-=2){
if (p == 2) {
sum+=(Boat[0] + Boat[1] + Boat[2]);
break;
} else if (p == 1) {
sum+=Boat[1];
break;
} else if (p == 0) {
sum+=Boat[0];
break;
}
else {
int sum1=2*Boat[0]+Boat[p]+Boat[p-1];
int sum2=Boat[p]+2* Boat[1]+Boat[0];
if(sum1<sum2){
sum+=sum1;
}else {
sum+=sum2;
}
}
}
System.out.println(sum);
}
}
}
}