原题:
假设三位学生为a(a1,a2,a3),b(b1,b2,b3),c(c1,c2,c3),由样例说明可知:
a用时:a1+a2=10000+10000=20000
b用时:a1+a2+a3+(b1+b2)=10000+10000+10000+(30000+20000)=80000
c用时:a1+a2+a3+(b1+b2+b3)+(c1+c2)=10000+10000+10000+30000+20000+30000+20000+50000=180000
因此最小之和是20000+80000+180000=280000
思路了解,下面便是代码实现:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Student> list = new ArrayList<Student>();
// 接收学生所用的时间s、a、e
for(int i = 0; i < n; i++) {
list.add(new Student(sc.nextInt(),sc.nextInt(),sc.nextInt()));
}
// 用比较器排序,按s + a用时最少到最多排列
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.sum + o1.e > o2.sum + o2.e) {
return 1;
} else if(o1.sum + o1.e < o2.sum + o2.e) {
return -1;
}
return 0;
}
});
long ans = 0;
long time = 0;
for(int i = 0; i < n; i++) {
// 这里是答题用的时间sum由Student构造方法可知sum = s + a
ans += time += list.get(i).sum;
// 这里sum = sum + e
time += list.get(i).e;
}
System.out.println(ans);
sc.close();
}
static class Student {
int s;
int a;
int e;
int sum;
public Student(int s, int a, int e) {
this.s = s;
this.a = a;
this.e = e;
this.sum = this.s + this.a;
}
}
}