'commit'
This commit is contained in:
73
TangDou/AcWing/913.md
Normal file
73
TangDou/AcWing/913.md
Normal file
@@ -0,0 +1,73 @@
|
||||
##[$AcWing$ $913$. 排队打水](https://www.acwing.com/problem/content/description/915/)
|
||||
|
||||
### 一、题目描述
|
||||
有 $n$ 个人排队到 $1$ 个水龙头处打水,第 $i$ 个人装满水桶所需的时间是 $t_i$,请问如何安排他们的打水顺序才能使所有人的等待时间之和最小?
|
||||
|
||||
**输入格式**
|
||||
第一行包含整数 $n$。
|
||||
|
||||
第二行包含 $n$ 个整数,其中第 $i$ 个整数表示第 $i$ 个人装满水桶所花费的时间 $t_i$。
|
||||
|
||||
**输出格式**
|
||||
输出一个整数,表示最小的等待时间之和。
|
||||
|
||||
**数据范围**
|
||||
$1≤n≤10^5$,
|
||||
$1≤t_i≤10^4$
|
||||
|
||||
**输入样例**:
|
||||
```cpp {.line-numbers}
|
||||
7
|
||||
3 6 1 4 2 5 7
|
||||
```
|
||||
|
||||
**输出样例**:
|
||||
```cpp {.line-numbers}
|
||||
56
|
||||
```
|
||||
|
||||
### 二、分析题意
|
||||
|
||||
看样例:
|
||||

|
||||
|
||||
$sum=3 * 6+6 * 5+1 * 4+4 * 3+2 * 2+5 * 1 $
|
||||
|
||||
<font color='red' size=4><b>解释:</b></font>
|
||||
$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]$$
|
||||
|
||||
|
||||
### 三、算法思路
|
||||
让最磨叽的人,最后打水,谁快就谁先来,节约大家时间。
|
||||
这样,数小的乘的倍数就多,数大的倍数就少,最终的结果就越小。
|
||||
|
||||
### 四、完整代码
|
||||
```cpp {.line-numbers}
|
||||
#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;
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user