Best Cow Fences
二分答案 + 前缀和
个人认为题意没有表述清楚,本题要求的是满足题意的连续子序列(难度大大降低了有木有)。
本题的精度也是非常令人陶醉,请您自行体会吧!
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
//Mystery_Sky
//
#define M 10000100
#define ex 1e-5
double l = 100000.0, r = -10000.0, mid;
int n, L;
double a[M], sum[M];
inline bool check(double ans)
{
double sum1, sum2, maxx;
sum1 = sum[L-1] - (L-1) * ans;
for(int i = L; i <= n; i++) {
sum2 = sum[i] - sum[i - L] - L * ans;
sum1 = sum1 + a[i] - ans;
sum1 = max(sum1, sum2);
if(sum1 > -ex) return true;
}
return false;
}
int main() {
scanf("%d%d", &n, &L);
for(int i = 1; i <= n; i++) {
scanf("%lf", &a[i]);
sum[i] = sum[i-1] + a[i];
l = min(l, a[i]), r = max(r, a[i]);
}
while(r - l > ex) {
mid = (l + r)/2;
if(check(mid)) l = mid;
else r = mid;
}
int ans = (int)(r * 1000);
printf("%d", ans);
return 0;
}
原文地址:https://www.cnblogs.com/Benjamin-cpp/p/10878188.html
时间: 2024-10-12 10:37:37