You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

##AcWing 913. 排队打水

一、题目描述

n 个人排队到 1 个水龙头处打水,第 i 个人装满水桶所需的时间是 t_i,请问如何安排他们的打水顺序才能使所有人的等待时间之和最小?

输入格式 第一行包含整数 n

第二行包含 n 个整数,其中第 i 个整数表示第 i 个人装满水桶所花费的时间 t_i

输出格式 输出一个整数,表示最小的等待时间之和。

数据范围 1≤n≤10^5, 1≤t_i≤10^4

输入样例

7
3 6 1 4 2 5 7

输出样例

56

二、分析题意

看样例: 2.png

sum=3 * 6+6 * 5+1 * 4+4 * 3+2 * 2+5 * 1

解释: 3在第一位,后面6个人都需要等待,+3*6 6在第二位,后面5个人都需要等待,+6*5 1在第三位,后面4个人都需要等待,+1*4 4在第四位,后面3个人都需要等待,+4*3 2在第五位,后面2个人都需要等待,+2*2 5在第六位,后面1个人都需要等待,+5*1 7在第七位,后面没有人需要等待,截止

公式

\large sum=\sum\limits_{i=1}^{n}(n-i+1)*a[i]

三、算法思路

让最磨叽的人,最后打水,谁快就谁先来,节约大家时间。 这样,数小的乘的倍数就多,数大的倍数就少,最终的结果就越小。

四、完整代码

#include <bits/stdc++.h>

using namespace std;
const int N = 100010;

typedef long long LL;
int a[N];
LL res;

int main() {
    int n;
    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 - i - 1);
    printf("%lld", res);
    return 0;
}