Best Cow Fences
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16945   Accepted: 5425

Description

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

Source

OJ-ID:
poj-2018

author:
Caution_X

date of submission:
20191118

tags:
二分

description modelling:
给定正整数数列A[],求一个平均值最大的,长度不小于L的(连续的)子段,输出该平均值*1000

major steps to solve it:
(1)二分枚举所有的平均值
(2)每一次二分,将A[]-mid,之后求前缀和。判断所取子段平均值是否大于等于mid只要判断选取区间累加是否大于等于0即可

AC code:

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
double a[],b[];
double sum[];
double eps=1e-;
int main()
{
//freopen("input.txt","r",stdin);
int N,L;
scanf("%d%d",&N,&L);
for(int i=;i<=N;i++)
{
scanf("%lf",&a[i]);
}
double l=-1e6,r=1e6;
while(r-l>eps)
{
double mid=(r+l)/;
for(int i=;i<=N;i++) b[i]=a[i]-mid;
for(int i=;i<=N;i++)
{
sum[i]=sum[i-]+b[i];
}
double ans=-1e10;
double min_val=1e10;
for(int i=L;i<=N;i++)
{
min_val=min(min_val,sum[i-L]);
ans=max(ans,sum[i]-min_val);
}
if(ans>=) l=mid;
else r=mid;
}
printf("%d\n",int(r*));
}

POJ-2018 Authors Register Update your info Authors ranklist的更多相关文章

  1. 【POJ 2018】 Best Cow Fences

    [题目链接] http://poj.org/problem?id=2018 [算法] 二分平均值 检验时将每个数减去二分的值,求长度至少为L的子序列和的最大值,判断是否大于0 [代码] #includ ...

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

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

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

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

  4. POJ 2018 Best Cow Fences (二分答案构造新权值 or 斜率优化)

    $ POJ~2018~Best~Cow~ Fences $(二分答案构造新权值) $ solution: $ 题目大意: 给定正整数数列 $ A $ ,求一个平均数最大的长度不小于 $ L $ 的子段 ...

  5. Best Cow Fences POJ - 2018 (二分)

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

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

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

  7. POJ 2018 Best Cow Fences(二分最大区间平均数)题解

    题意:给出长度>=f的最大连续区间平均数 思路:二分这个平均数,然后O(n)判断是否可行,再调整l,r.判断方法是,先求出每个数对这个平均数的贡献,再求出长度>=f的最大贡献的区间,如果这 ...

  8. POJ 2018 Best Cow Fences

    斜率优化. 设$s[i]$表示前缀和,$avg(i,j)=(s[j]-s[i-1])/(j-(i-1))$.就是$(j,s[j])$与$(i-1,s[i-1])$两点之间的斜率. 如果,我们目前在计算 ...

  9. POJ 2018

    又一水,设dp[i]为以i结尾的有最大平均值的起始位置. #include <iostream> #include <cstdio> #include <cstring& ...

随机推荐

  1. python学习-class封装

    # 封装 类=属性+行为 抽像 -class StudentV2: # 类属性 所有的实例可以共享 .不属于任何实例的特性. is_people = True # 类方法 1.装饰器.2.参数是cls ...

  2. AJAX中的dataType

    参考ajax的api文档 dataType的类型:String ajax中的dataType的属性: text:返回纯文本字符串 json:返回json数据 jsonp:jsonp格式(我没用过) h ...

  3. JS基础知识——原型与原型链

    1.如何准确判断一个变量的数组类型 2.写一个原型链继承的例子 3.描述new一个对象的过程 4.zepto(或其他框架中如何使用原型链) 知识点: (1)构造函数 function Foo(name ...

  4. poj 1741 Tree (树的分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 30928   Accepted: 10351 Descriptio ...

  5. IJKPlayerView设置Header播放视频的方法

    播放b站视频连接的实测图 https://github.com/Rukey7/IjkPlayerView 使用库的连接 在用这个库播放b站视频连接的时候总是播放不了 检查了一下是因为b站视频连接需要验 ...

  6. JVM 学习笔记二 :JVM内存区域

    一.内存分配概述

  7. PyTorch-网络的创建,预训练模型的加载

    本文是PyTorch使用过程中的的一些总结,有以下内容: 构建网络模型的方法 网络层的遍历 各层参数的遍历 模型的保存与加载 从预训练模型为网络参数赋值 主要涉及到以下函数的使用 add_module ...

  8. JS---封装缓动(变速)动画函数---增加任意多个属性&增加回调函数

    封装缓动(变速)动画函数---增加任意多个属性&增加回调函数 回掉函数fn,在所有元素到达目的位置后,判断是否传入一个函数,有就调用 if(fn){fn()}; 这样一次点击,产生多个动画 & ...

  9. JS 获取元素、修改元素/css样式/标签属性、简单事件、数据类型

    基本使用 写在Script 标签里 引入外部js文件:<script src=" "></script> console.log(" " ...

  10. Android 插件化开发(三):资源插件化

    在前面的文章中我们成功的加载了外部的Dex(Apk)并执行了插件的Bean代码.这时我们会想,能不能加载并运行插件Apk的Activity.答案当然是能,否则后续我们的研究就没意义了,但是想实现Act ...