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. js方法中参数传过来的值包含括号

    前提,传递的id为变量值,比如从后台获取数据循环,在每个循环里调用shenpi()方法,假设传的id包含括号,例如 20190329100833(更正) 这样的数据,那么直接调用会报错,控制台会报错: ...

  2. 廉价OpenVZ的VPS如何在solusvm下保证永不死

    行业里面有openvz架构的其实是一个不错的架构,资源的利用效率挺高的,当然也有一些限制,同时也带来一些缺点,其中最大的缺点莫过于超售了,卖1G的RAM可能连128都没有,这样的直接后果就是某些不良玩 ...

  3. Windows10 中的字母映射表

    有很多朋友为寻找特殊字符串而感到烦恼, windows10中的字符映射表有所有字体 包含的特殊符号 windows键 + R键 输入 charmap 点击确定 即可出现 字母映射表 可在字符的下拉按钮 ...

  4. 使用where 过滤数据

    --本章主要内容是MySQL中使用where搜索条件进行过滤数据. where条件在from子句后面给出,如下所示: select name,price from shops where price& ...

  5. C# DataTable 某一列取算

    1. 列为数字类型double total= Convert.ToDouble(datatable.Compute("SUM(需要求和的参数)", "")); ...

  6. 在.Net Core中记录日志

    一个完善的系统,必然会有非常完善的日志记录,用户的操作.系统的运行状况等信息被完整的记录下来,方便我们对系统进行维护和改进..net core 也为日志记录提供了内置的支持. 在控制台程序中记录日志 ...

  7. VS2019 开发Django(五)------createsuperuser

    导航:VS2019开发Django系列 上篇我们已经把LazyOrders中用到的C#的实体转成了Django中的Entity,并且已经迁移数据库成功,那么,今天继续介绍Django中内置的数据库操作 ...

  8. SpringCloud服务过滤filter

    一.目录展示 二.application.yml配置文件 三.MyZuulFilter package com.zn.filter; import com.netflix.zuul.ZuulFilte ...

  9. Hive 性能测试工具 hive-testbench

    下载: yum -y install gcc gcc-c++ maven 下载地址Github:https://github.com/hortonworks/hive-testbench/git cl ...

  10. FCC---CSS Flexbox: Add Flex Superpowers to the Tweet Embed

    To the right is the tweet embed that will be used as the practical example. Some of the elements wou ...