Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains a certain number of cows, 1 <= ncows <= 2000.

FJ wants to build a fence around a contiguous group of these fields in order to maximize the average number of cows per field within that block. The block must contain at least F (1 <= F <= N) fields, where F given as input.

Calculate the fence placement that maximizes the average, given the constraint.

Input

* Line 1: Two space-separated integers, N and F.

* Lines 2..N+1: Each line contains a single integer, the number of cows in a field. Line 2 gives the number of cows in field 1,line 3 gives the number in field 2, and so on.

Output

* Line 1: A single integer that is 1000 times the maximal average.Do not perform rounding, just print the integer that is 1000*ncows/nfields.

Sample Input

10 6
6
4
2
10
3
8
5
9
4
1

Sample Output

6500

题意:n块田地,每块田地有cows【i】头牛,求出一个长度不小于F的子段,使子段牛的平均数最大。
思路:我们令 avr = sum【i,j】/(i-j+1)
那么这题就是 求是avr的最大值,我们二分枚举ans,判断 avr 是否不小于 ans,即avr >= ans,为了不维护(i-j+1)的值,变形成sum【i,j】 - ans*(i-j+1) >= 0,
我们把原数组cows【i】-ans,那么Sum【i,j】 = sum【i,j】-ans(i-j+1)。
现在关键就是取得一个  max{Sum【i,j】},对于Sum【i,j】,我们可以用前缀和相减的方式求得,sum(i)-min(sum(j)), 0 <= j <= i-F。 坑点:注意最后答应的是二分的R值,我打印L值Wa的怀疑人生.而且题目要求精度是1e-4,当我们枚举的精度不小于题目要求精度的时候L和R值都是OK的
我感觉是如果两个值转换为整数不同的话,R值转换的整数值在L~R区间内的,而l转换的值是小于l的,如果两个值转换的值相同,打印那个都行,且整数肯定小于L。 其实像是最大值最小,最小值最大,都可以用二分解决,答案是单调的,这题还有种用凸包方法写的,以后再填坑吧。
 #include<cstdio>
#include<iostream>
using namespace std; const int maxn = 1e5+;
int n,f;
int cows[maxn];
const double eps = 1e-; bool solve(double x,int f)
{
double fcows[n+];
double sum[n+];
for(int i=;i<=n;i++)fcows[i] = cows[i] - x;
for(int i=;i<=n;i++)sum[i] = sum[i-]+fcows[i];
double ans = -1e10,minn = 1e10;
for(int i=f;i<=n;i++)
{
minn = min(minn,sum[i-f]);
ans = max(ans,sum[i]-minn);
}
return ans >= ;
}
int main()
{
scanf("%d%d",&n,&f);
double low=;
double high = ;
for(int i=;i<=n;i++)
{
scanf("%d",&cows[i]);
high += cows[i];
}
while(low + eps < high)
{
double mid = (low+high)/;
if(solve(mid,f))low = mid;
else high = mid;
}
printf("%d\n",(int)(*high));
}

Best Cow Fences POJ - 2018 (二分)的更多相关文章

  1. POJ - 2018 二分+单调子段和

    依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...

  2. POJ 2018 Best Cow Fences(二分+最大连续子段和)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14601 Accepted: 4720 Desc ...

  3. Poj 2018 Best Cow Fences(分数规划+DP&&斜率优化)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Description Farmer John's farm consists of a ...

  4. POJ-2018 Best Cow Fences(二分加DP)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10174 Accepted: 3294 Desc ...

  5. loj#10012\poj2018 Best Cow Fences(二分)

    题目 #10012 「一本通 1.2 例 2」Best Cow Fences 解析 有序列\(\{a_i\}\),设\([l,r]\)上的平均值为\(\bar{x}\),有\(\sum_{i=l}^r ...

  6. 一本通 1434:【例题2】Best Cow Fences

    Best Cow Fences 二分答案 + 前缀和 个人认为题意没有表述清楚,本题要求的是满足题意的连续子序列(难度大大降低了有木有). 本题的精度也是非常令人陶醉,请您自行体会吧! #includ ...

  7. 1434:【例题2】Best Cow Fences

    1434:[例题2]Best Cow Fences 时间限制: 1000 ms         内存限制: 65536 KB提交数: 263     通过数: 146 [题目描述] 给定一个长度为n的 ...

  8. POJ2018 Best Cow Fences —— 斜率优化DP

    题目链接:https://vjudge.net/problem/POJ-2018 Best Cow Fences Time Limit: 1000MS   Memory Limit: 30000K T ...

  9. POJ 2018 Best Cow Fences(二分答案)

    题目链接:http://poj.org/problem?id=2018 题目给了一些农场,每个农场有一定数量的奶牛,农场依次排列,问选择至少连续排列F个农场的序列,使这些农场的奶牛平均数量最大,求最大 ...

随机推荐

  1. Web框架之Bootstrap

    一.Bootstrap简介 Bootstrap,来自Twitter的设计师Mark Otto和Jacob Thornton合作开发,是目前很受欢迎的Web前端开源框架. Bootstrap 是基于HT ...

  2. 快速理解VirtualBox的四种网络连接方式

    VirtualBox中有4中网络连接方式: NAT Bridged Adapter Internal Host-only Adapter VMWare中有三种,其实他跟VMWare 的网络连接方式都是 ...

  3. 【python】给正则匹配部分命名

    可以用?P<name>的方法给正则匹配的部分命名. 例:要将<字母,数字>的部分命名为test x = "abc <haha,123> test @@&q ...

  4. CF1019C

    好玄学的东西... 核心思想:for循环! 首先,我们从前向后扫所有的点,如果这个点没被标记成不可用就把这个点标记成已使用,然后把所有与这个点直接相连的点标记成不可用 接下来,我们从后向前扫所有的点, ...

  5. node.js 的页面渲染方法ejs

    .安装依赖的组件 npm i consolidate -D npm i ejs -D 2.布局服务端 const express = require('express'); const consoli ...

  6. jenkins持续集成:构建多个job同时执行

    在jenkins 构建任务时,同时只能构建2个,但是有时候可能涉及到需要同时执行多个任务(大于2个),如果不能同时运行的话,就需要等待上一个执行完了,再执行第三个 比如用例非常多,需要把不同的用例分给 ...

  7. vue和stylus在subline中显示高亮

    首先: 安装这两个插件   Vue Syntax Highlight    和    stylus 1.按住 ctrl + shift + p 2.输入:install Package 3.输入: V ...

  8. Allegro PCB Design GXL (legacy) 元器件的坐标文件

    Allegro PCB Design GXL (legacy) version 16.6-2015 一.菜单:Tools > Reports... 二.在“Available Reports ( ...

  9. rsync启动并生成PID

    /usr/bin/rsync --daemon --config=/usr/local/rsync/etc/rsyncd.conf

  10. 阿里云服务器 http 转 https

    转载: http://blog.csdn.net/zzp961224/article/details/78934310 做个笔记 以备遗忘. 环境: 阿里云云服务器    Windows Server ...