[AcWing 913] 排队打水

点击查看代码
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int n;
LL res;
int a[N];
int main()
{
cin >> n;
for (int i = 0; i < n; i ++) cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n; i ++) res += a[i] * (n - 1 - i);
printf("%lld\n", res);
return 0;
}
- 算法思路
$ f = \sum_{1}^{n} t_i \times (n - i) $ ,
\(t_i\) 相当于是权值,后面的 \((n - i)\) 是不变的,让时间短的先打水可使 \(f\) 最小

浙公网安备 33010602011771号