题目:
有 n 个人排队到 1 个水龙头处打水,第 i 个人装满水桶所需的时间是 ti,请问如何安排他们的打水顺序才能使所有人的等待时间之和最小?
第一行包含整数 n。
第二行包含 n 个整数,其中第 i 个整数表示第 i 个人装满水桶所花费的时间 ti。
输出一个整数,表示最小的等待时间之和。
1 ≤ n ≤ 10^5
1 ≤ ti ≤ 10^4
输入:
7
3 6 1 4 2 5 7
输出:
56
public class 排队打水 {
public static int N = 1000010, n;
public static int[] t = new int[N];
public static void main(String[] args) {
//接收
Scanner in = new Scanner(System.in);
n = in.nextInt();
for (int i = 0; i<n; i++) t[i] = in.nextInt();
//排序
Arrays.sort(t, 0, n);
//计算总时间
long res = 0;
for (int i = 0; i<n; i++) res += (t[i] * (n-1 - i));
System.out.println(res);
}
}
思路:
太简单了,没什么可说的
有点类似于cpu任务进程,计算等待时间
声明:
算法思路来源为y总,详细请见https://www.acwing.com/
本文仅用作学习记录和交流