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的情况下。

  1. #include <iostream>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. using namespace std;
  7. #define MAX 100000
  8. double a[MAX+5];
  9. double s[MAX+5];
  10. int n,f;
  11. int fun(double ave)
  12. {
  13. double num1=s[f-1]-(f-1)*ave;
  14. for(int i=f;i<=n;i++)
  15. {
  16. double num2=s[i]-s[i-f]-f*ave;
  17. num1=num1+a[i]-ave;
  18. num1=max(num1,num2);
  19. if(num1>-1e-6)
  20. return 1;
  21. }
  22. return 0;
  23. }
  24. int main()
  25. {
  26. double l,r;
  27. while(scanf("%d%d",&n,&f)!=EOF)
  28. {
  29. l=2000;
  30. r=-1;
  31. memset(s,0,sizeof(s));
  32. for(int i=1;i<=n;i++)
  33. {
  34. scanf("%lf",&a[i]);
  35. s[i]=s[i-1]+a[i];
  36. r=max(r,a[i]);
  37. l=min(l,a[i]);
  38. }
  39. double mid;
  40. while(r-l>=1e-6)
  41. {
  42. mid=(l+r)/2;
  43. if(fun(mid))
  44. {
  45. l=mid;
  46. }
  47. else
  48. r=mid;
  49. }
  50. printf("%d\n",(int)(r*1000));
  51. }
  52. return 0;
  53. }

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. 5 -- Hibernate的基本用法 --4 5 JNDI数据源的连接属性

    如果无须Hibernate自己管理数据源,而是直接访问容器管理数据源,Hibernate可使用JNDI(Java Naming Directory Interface,Java命名目录接口)数据源的相 ...

  2. Eclipse cdt解决github导入的项目无法打开声明的bug (cannot open declaration)

    概述: 我利用eclipse 的git插件clone github上的远程项目(C++)到本地时遇到一个问题:clone下来的项目没有C++特性,无法使用open declaration等操作,下面是 ...

  3. NetBpm 目录

    整理了一下网上的一些netbpm,虽然这项目现在不再更新了,还是想去学习一下,这个组件用时候很方便,比workFlow方便的多了 如果像jbpm那样一直更新就好了. 前两篇是个人的一个总结,后面一些是 ...

  4. html+jquery制作网页地图

    http://jvectormap.com/ <!--StartFragment --> JVectorMap 是一个显示矢量地图的jQuery插件.它使用 SVG 在Firefox 3 ...

  5. windows本地hash值获取和破解详解

    powershell版的procdump https://www.t00ls.net/articles-48428.html procdump procdump是微软官方提供的一个小工具, 微软官方下 ...

  6. 【RF库XML测试】测试的XML文件说明

    文件存放路径:C:\workspace\robotframework\test_rf_api\testdata\XML.xml 文件内容: <example> <first id=& ...

  7. mysql类型对应Java的类型

    整型 JDBCtinyint         java.lang.Integersmallintmediumint       java.lang.Longint          bigint    ...

  8. react中的hoc和修饰器@connect结合使用

    在学习react-redux的时候,看到了修饰器这个新的属性,这个是es7的提案属性,很方便.于是我用@connect代替了connect(使用的时候需要配置,这里不赘述),省去了很多不必要的代码,但 ...

  9. springmvc接收前台(可以是ajax)传来的数组list,map,set等集合,复杂对象集合等图文详解

    参考帖子: http://blog.csdn.net/wabiaozia/article/details/50803581 方法参考: { "token":"" ...

  10. linux批量修改文件名

    源文件; [root@test_machine fuzj]# ls fuzj-1.txt  fuzj-2.txt  fuzj-3.txt  fuzj-4.txt  fuzj-5.txt  fuzj-6 ...