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. 深入学习CSS3-flexbox布局

    学习博客:https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 学习博客:http://caibaojian.com/demo/flexbox ...

  2. ansible部署apache

    Ansible 配置Apache(加粗的字体是要执行的命令) 配置免密登录 [root@localhost ansible]# vim /etc/ansible/hosts //用来配置目标主机 加入 ...

  3. Java内存模型之原子性问题

    本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 前言 之前的文章中讲到,JMM是内存模型规范在Java语 ...

  4. 《Java Spring框架》Spring切面(AOP)配置详解

    1.  Spring 基本概念 AOP(Aspect Oriented Programming)称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2 ...

  5. 下载Abook 高等教育出版社网站资料

    一.背景 又快到了期末复习周,这个学期学了一门操作系统,老师没有给课本习题的答案,说是配套网站上有,我看了一下,确实有,是高等教育出版社的数字课程网站Abookl http://abook.hep.c ...

  6. Java面试题_第四阶段

    1.1 电商行业特点 1.分布式 垂直拆分:根据功能模块进行拆分 水平拆分:根据业务层级进行拆分 2.高并发 用户单位时间内访问服务器数量,是电商行业中面临的主要问题 3.集群 抗击高兵发的有效手段, ...

  7. Csharp:HttpWebRequest or HttpClient

    /// <summary> /// Define other methods and classes here /// </summary> /// <param nam ...

  8. CSDN不限积分代下载,知网、万方、sci、IEEE论文代下载,智慧树、超星尔雅刷课

    下载内容: 1.CSDN不限积分代下载. 2.知网.万方.sci.IEEE论文代下载. 3.超星尔雅,智慧树刷课. 注:由于本人手抖买一个CSDN会员,想挽回一点损失,所以创立了一个下载群,绝对不是骗 ...

  9. Leetcode刷题笔记(Python 找出所有相加之和为n的k个组合,组合中只允许含有1-9的正整数,并且每种组合中不存在重复的数字。)

    eg:输入:k=3,n=9 输出: [[1,2,6],[1,3,5],[2,3,4]] 输入:k=2,n=5 输出:[[1,4][2,3]] #!/usr/bin/env python # -*- c ...

  10. C语言函数调用过程,汇编角度查看

    C语言函数调用过程,汇编角度查看 把函数的参数按照调用约定压栈或者存储到寄存器中 调用要使用的函数,先把调用者的地址入栈,方便回来 跳转到函数 把函数使用到的一些寄存器压栈,避免修改寄存器的值 执行函 ...