排序不等式
- 问题背景
有 n 个人排队到 11 个水龙头处打水,第 i 个人装满水桶所需的时间是 ti,请问如何安排他们的打水顺序才能使所有人的等待时间之和最小?
- 策略
- 让用时长短的人先装
练习
01 排队打水
- 题目
- 题解
import java.io.*;
import java.util.*;
public class Main {
public static final int N = 100010;
public static int[] t = new int[N];
public static int n;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
n = Integer.parseInt(br.readLine());
String[] str1 = br.readLine().split(" ");
for (int i = 1; i <= n; i++) {
t[i] = Integer.parseInt(str1[i - 1]);
}
Arrays.sort(t, 1, n + 1);
long res = 0;
for (int i = 1; i <= n; i++) {
res += t[i] * (n - i);
}
pw.println(res);
pw.close();
br.close();
}
}