P1223. 排队接水

考点

  • 贪心

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bits/stdc++.h>

using namespace std;

const int LEN = 1050;

struct Node
{
int t_, id_;
} arr[LEN];

int main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> arr[i].t_;
arr[i].id_ = i + 1;
}
sort(arr, arr + n, [](Node a, Node b) -> bool
{ return a.t_ < b.t_; });
double sum = 0;
for (int i = 0; i < n; ++i)
{
cout << arr[i].id_ << " ";
sum += arr[i].t_ * (n - i - 1);
}
cout << endl
<< fixed << setprecision(2) << sum / n;
return 0;
}

思路

排队接水,显然前面的人用时越短,后面的人等的时间就越短

因为大家都站着等你呐!

题目这里表达不算清晰,求的是除每次打水的人之外,其余人等待时间的平均值

所以计算的时候本人打水时间不要算进去