2020.12.30开始学习AcWing算法《算法竞赛进阶指南》;
上传博客方便复习。
#include <iostream>
using namespace std;
#include <algorithm>
const int N = 100001;
int n, m;
int cow[N];
double sum[N];bool check (double ave) {for (int i = 1; i <= n; i++) {sum[i] = sum[i - 1] + cow[i] - ave;}double mins = 0;for (int i = 0, j = m; j <= n; j++, i++) {mins = min(sum[i], mins);if (sum[j] >= mins) {return true;}}return false;
}int main() {double r = 0, l = 0;cin >> n >> m;for (int i = 1; i <= n; i++) {cin >> cow[i];r = max(r, double(cow[i]));//小优化}
// double r = 2000, l = 0;//未优化while (r - l > 1e-5) {double mid = (l + r) / 2;if (check(mid)) {l = mid;} else {r = mid;}}cout << int (r * 1000) << endl;return 0;
}