Best Cow Fences

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 10174 Accepted: 3294

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

二分加DP的题目前面也做到过一道题目。选取序列的最大值和最小值,平均值在二者之间。然后二分。判断这个值是比答案的大还是小,就要用到dp。把数列的每个值都减去这个平均值,如果存在区间和大于等于0,说明这个数列存在平均在比这个还大的。用dp的思想来求这个序列最大区间和,注意区间长度要大于等于f的情况下。

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h> using namespace std;
#define MAX 100000
double a[MAX+5];
double s[MAX+5];
int n,f;
int fun(double ave)
{ double num1=s[f-1]-(f-1)*ave;
for(int i=f;i<=n;i++)
{
double num2=s[i]-s[i-f]-f*ave;
num1=num1+a[i]-ave;
num1=max(num1,num2);
if(num1>-1e-6)
return 1;
}
return 0;
}
int main()
{
double l,r;
while(scanf("%d%d",&n,&f)!=EOF)
{
l=2000;
r=-1; memset(s,0,sizeof(s)); for(int i=1;i<=n;i++)
{
scanf("%lf",&a[i]);
s[i]=s[i-1]+a[i];
r=max(r,a[i]);
l=min(l,a[i]);
}
double mid;
while(r-l>=1e-6)
{
mid=(l+r)/2;
if(fun(mid))
{
l=mid;
}
else
r=mid;
}
printf("%d\n",(int)(r*1000));
}
return 0;
}

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

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

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

  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 (二分答案构造新权值 or 斜率优化)

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

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

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

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

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

  6. 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])$两点之间的斜率. 如果,我们目前在计算 ...

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

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

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

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

  9. poj2018 Best Cow Fences[二分答案or凸包优化]

    题目. 首先暴力很好搞,但是优化的话就不会了.放弃QWQ. 做法1:二分答案 然后发现平均值是$ave=\frac{sum}{len}$,这种形式似乎可以二分答案?把$len$移到左边. 于是二分$a ...

随机推荐

  1. golang 内存分析/动态追踪

    如果你的go程序是用http包启动的web服务器,你想查看自己的web服务器的状态.这个时候就可以选择net/http/pprof.你只需要引入包_"net/http/pprof" ...

  2. golang-defer坑的本质

    https://blog.csdn.net/hittata/article/details/77836435

  3. 为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?【转载】

    转载自:http://www.cnblogs.com/dancefire/archive/2013/01/02/why-notifyDataSetChanged-does-not-work.html ...

  4. ios开发之--使用xib适配iPhone X

    最近在修改一个老项目,里面有很多xib文件,需要适配iPhone X,但是又不想重写页面用代码适配,分享个小方法,也算是个笨办法吧, 适配iPhone X底部,iPhone X底部有34px的操作区域 ...

  5. Python生成器笔记

    Python中三大器有迭代器,生成器,装饰器,本文主要讲述生成器.主要从生成器的概念,本质,以及yield关键字的使用执行过程. 本质:生成器是一类特殊的迭代器,使用了yield关键字的函数不再是函数 ...

  6. iOS 静态库和动态库(库详解)

    什么是库 ? 库就是程序代码的集合,将N个文件组织起来,是共享程序代码的一种方式.库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行. 库的分类 开源库:源代码是公开的,可以看到每个实现 ...

  7. PHP垃圾回收机制防止内存溢出

    PHP语言同其他语言一样,具有垃圾回收机制.那么今天我们要为大家讲解的内容就是关于PHP垃圾回收机制的相关问题.希望对大家有所帮助. 一.PHP 垃圾回收机制(Garbage Collector 简称 ...

  8. C#后台传入数据JS接收

    今天碰到个问题,就是后台传入的数据,在JS中for循环的时候,下面那个j根本就不会往上加.所以只能将后台传入的对象,转换为json格式,由js进行解析后生成js中的对象 @{j=0;} for (va ...

  9. Linux记录用户shell命令

    在/etc/profile中添加下面内容: export LC_ALL=C TMOUT=3600 HISTFILESIZE=2000 HISTSIZE=2000 HISTTIMEFORMAT=&quo ...

  10. thinkphp3.2 实现上一篇和下一篇

    现在在做一个能够在内容页点击上一篇可以看到上一篇,点击下一篇可以看到下一篇. 首先http://www.mmkb.com/zhendao/index/news_show?code=98 现在code= ...