-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem0102.h
56 lines (50 loc) · 1.59 KB
/
Problem0102.h
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// Created by Fengwei Zhang on 10/31/21.
//
#ifndef ACWINGSOLUTION_PROBLEM0102_H
#define ACWINGSOLUTION_PROBLEM0102_H
#include <iostream>
#include <cstring>
using namespace std;
class Problem0102 {
// https://www.acwing.com/solution/content/1148/
// 注意2个浮点数的比较方式,注意"零值"的精度
private:
bool CheckAvg(const int cows[], const int &n, const int &f, const double &avg) {
double prefix_sum[n + 1];
memset(prefix_sum, 0, sizeof prefix_sum);
// 利用前缀和,巧妙的解法,类似滑动窗口:优化O(n^2)到O(n)
for (int i = 1; i <= n; ++i) {
prefix_sum[i] = prefix_sum[i - 1] + (cows[i] - avg);
}
double pre_min = prefix_sum[0];
for (int i = 1, j = f; j <= n; ++i, ++j) {
pre_min = min(pre_min, prefix_sum[i - 1]); // 只检验最大值,只需比较和pre_min的差
if (prefix_sum[j] - pre_min > 1e-9) {
return true;
}
}
return false;
}
int main() {
int n, f;
scanf("%d%d", &n, &f);
int cows[n + 1];
double l = 0, r = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", &cows[i]);
r = max(r, (double) cows[i]);
}
while (r - l > 1e-5) { // 二分查找浮点数
auto mid = l + (r - l) / 2;
if (CheckAvg(cows, n, f, mid)) {
l = mid;
} else {
r = mid;
}
}
printf("%d\n", (int) (r * 1000));
return 0;
}
};
#endif //ACWINGSOLUTION_PROBLEM0102_H