AcWing 102. 最佳牛围栏
原题链接
简单
作者:
MILLOPE
,
2019-03-25 19:33:47
,
所有人可见
,
阅读 1085
二分
C++ 代码
#include <bits/stdc++.h>
const double eps = 1e-5;
const int maxn = 1e5 + 100;
typedef long long LL;
template <typename T>
inline void read(T &s) {
s = 0;
T w = 1, ch = getchar();
while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
s *= w;
}
int n;
int f;
int a[maxn];
double l;
double r;
double mid;
double sum[maxn];
bool check(double avg) {
for (int i = 1; i <= n; ++i)
sum[i] = sum[i - 1] + a[i] - avg;
double minv = 0;
for (int i = 0, j = f; j <= n; ++i, ++j) {
minv = std::min(sum[i], minv);
if (sum[j] - minv >= 0) return true;
}
return false;
}
int main() {
l = 0, r = 0;
read(n), read(f);
for (int i = 1; i <= n; ++i) {
read(a[i]);
r = std::max(r, (double)a[i]);
}
while (r - l > eps) {
mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
printf("%d\n", (int)(r * 1000));
return 0;
}