1. 排队打水
题目链接: https://www.acwing.com/problem/content/description/915/
题目描述
有 n n n 个人排队到 1 1 1 个水龙头处打水,第 i i i 个人装满水桶所需的时间是 t i t_i ti,请问如何安排他们的打水顺序才能使所有人的等待时间之和最小?
输入格式
第一行包含整数
n
n
n。
第二行包含 n n n 个整数,其中第 i i i 个整数表示第 i i i 个人装满水桶所花费的时间 t i t_i ti。
输出格式
输出一个整数,表示最小的等待时间之和。
数据范围
1
≤
n
≤
1
0
5
,
1≤n≤10^5,
1≤n≤105,
1
≤
t
i
≤
1
0
4
1≤t_i≤10^4
1≤ti≤104
样例
输入样例:
7
3 6 1 4 2 5 7
输出样例:
56
参考文献
C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010;
int t[N];
int n;
int main()
{
scanf("%d", &n);
for (int i=0; i<n; i++) scanf("%d", &t[i]);
// 快速排序, 从小到大排队
sort(t, t+n);
// res 存储等待结果
LL res = 0;
for (int i=0; i<n; i++) res += t[i] * (n - i - 1);
printf("%lld\n", res);
return 0;
}