acwing 102 -利用二分枚举区间平均值
我真的是服了,看了一晚上发现居然,,,,,
上图吧,话说有人评论没。。。
对于结果来说,不一定要枚举有序数列,感觉这是一种猜结果的方法,只不过特别精确,令人发指
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #define maxn 110000 #define INF 0x3f3f3f3f using namespace std; double list[maxn]; double s[maxn]; int f, n; double l, r; int check(double mid) { //获得前缀和 for (int i = 1; i <= n; i++) { s[i] = list[i] - mid; s[i] += s[i - 1]; } double mi = 1e8; double mx = -1e8; for (int i = f; i <= n; i++) {//1---n之间枚举 mi = min(mi, s[i - f]);//get最小起点 mx = max(mx, s[i] - mi);//get最大区间和 } if (mx <= 0) {//mid不行,大了 return 0; } else return 1; return 0; } int main() { scanf("%d %d", &n, &f); for (int i = 1; i <= n; i++) { scanf("%lf", &list[i]); } l = -1e6; r = 1e6; double mid; while(r-l>1e-5) { mid = (l + r) / 2; if (check(mid)) {//mid可以,答案还可以更大 l = mid; } else { r = mid; } } printf("%d\n", (int)(r*1000)); return 0; }
寻找真正的热爱