[AcWing 913] 排队打水

image


点击查看代码
#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;
}

  1. 算法思路
    $ f = \sum_{1}^{n} t_i \times (n - i) $ ,
    \(t_i\) 相当于是权值,后面的 \((n - i)\) 是不变的,让时间短的先打水可使 \(f\) 最小
posted @ 2022-06-14 21:07  wKingYu  阅读(25)  评论(0)    收藏  举报