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

  1. 10 6
  2. 6
  3. 4
  4. 2
  5. 10
  6. 3
  7. 8
  8. 5
  9. 9
  10. 4
  11. 1

Sample Output

  1. 6500

题意:

给定一个正整数序列a,求一个平均数最大的、长度不小于L的子段。

Solution:

本题由于是求平均数的最大值,我们很容易往二分方向上想,二分答案的关键是如何取check,判定“是否存在一个长度不小与L的子段平均数不小于二分的值”。

考虑这样一种思路,把数列中每个数都减去二分的值,就转化为判断是否存在一个长不小于L的子段,子段和非负。继续思考,

1、若没有L的限制,只需O(n)扫描数列不断加数当和为负数时就把当前子段清空,扫描过程中出现过的最大子段和即为所求。

2、求一个子段它的和最大且长度不小与L。子段和可以转化为前缀和相减的形式,即设sumi表示ai~aj的和,则:

仔细观察上面的式子容易发现,随着i的增长,j的取值范围0~i-L每次只会增大1。换言之,每次只会有一个新的取值进入min{sumj}的候选集合,所以我们没有必要每次循环枚举j,只需要用一个变量记录当前的最小值,每次与新的取值sumi-L取min就可以了。

于是我们只需要看一下最大子段和是不是非负数就可以确定二分上下界的变化范围了。

(没看懂没事,第一遍我也没看懂,仔细看并对照代码,f**k真的简单巧妙!)

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<cstdlib>
  6. #define il inline
  7. #define ll long long
  8. #define debug printf("%d %s\n",__LINE__,__FUNCTION__)
  9. using namespace std;
  10. il int gi()
  11. {
  12. int a=;char x=getchar();bool f=;
  13. while((x<''||x>'')&&x!='-')x=getchar();
  14. if(x=='-')x=getchar(),f=;
  15. while(x>=''&&x<='')a=a*+x-,x=getchar();
  16. return f?-a:a;
  17. }
  18. const double eps=1e-;
  19. int n,L;
  20. double a[],b[],sum[];
  21. int main()
  22. {
  23. n=gi(),L=gi();
  24. for(int i=;i<=n;i++)a[i]=gi();
  25. double l=-1e6,r=1e6;
  26. while(r-l>eps){
  27. double mid=(l+r)/,ans=-1e10,minn=1e10;
  28. for(int i=;i<=n;i++)b[i]=a[i]-mid;
  29. for(int i=;i<=n;i++)sum[i]=sum[i-]+b[i];
  30. for(int i=L;i<=n;i++)minn=minn<sum[i-L]?minn:sum[i-L],ans=max(ans,sum[i]-minn);
  31. if(ans>=)l=mid;else r=mid;
  32. }
  33. printf("%d\n",int(r*));
  34. return ;
  35. }

poj2018——Best Cow Fences的更多相关文章

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

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

  2. POJ-2018 Best Cow Fences(二分加DP)

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

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

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

  4. [USACO2003][poj2018]Best Cow Fences(数形结合+单调队列维护)

    http://poj.org/problem?id=2018 此乃神题……详见04年集训队论文周源的,看了这个对斜率优化dp的理解也会好些. 分析: 我们要求的是{S[j]-s[i-1]}/{j-(i ...

  5. Poj2018 Best Cow Fences

    传送门 题目大意就是给定一个长度为 n 的正整数序列 A ,求一个平均数最大的,长度不小于 L 的子序列. 思路: 二分答案. Code: #include<iostream> #incl ...

  6. POJ2018 Best Cow Fences 二分

    实数折磨人啊啊啊啊啊啊啊 好,实数应该是最反人类的东西了...... 这个害得我调了0.5天才过. 大意是这样的:给你一个数列,求其中不少于f个的连续数的最大平均值. 不禁想起寒假的课程来... 此处 ...

  7. POJ-2018 Best Cow Fences 二分

    题意:找到一个连续区间,区间的长度至少大于f,现在要求这个区间的平均值最大. 题解: 二分找答案. 每次对于2分的mid值, 都把原来的区间减去mid, 然后找到一长度至少为f的区间, 他们的区间和& ...

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

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

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

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

随机推荐

  1. 1030: [JSOI2007]文本生成器

    1030: [JSOI2007]文本生成器 https://www.lydsy.com/JudgeOnline/problem.php?id=1030 分析: AC自动机+dp. 正难则反,求满足的, ...

  2. dubbo之main启动

    一.dubbo的main启动在使用上面会简单的多,但是需要做一些简单的配置. dubbo.spring.config=classpath*:META-INF/spring/*.xml 备注:这个是默认 ...

  3. javaweb(五)——Servlet开发(一)

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  4. hdu1847Good Luck in CET-4 Everybody!(sg函数)

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. MySQL日期、字符串、时间戳互转

    平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去搜索一下用法:本文将作为一个笔记,整理一下三者之间的 转换(即:date转字符串.date转时间戳.字符串转dat ...

  6. Linux命令应用大词典-第19章 文件系统管理

    19.1 mkfs:创建Linux文件系统 19.2 mke2fs:创建ext2.3.4文件系统 19.3 mkfs.ext4:创建ext4文件系统 19.4 mkfs.ext3:创建ext3文件系统 ...

  7. Linux命令应用大词典-第17章 软件包管理

    17.1 rpm:RPM软件包管理器 17.2 rpmargs:处理RPM软件包 17.3 rpmbuild:构建RPM软件包 17.4 rpmdiff:比较两个软件包之间的不同 17.5 rpmel ...

  8. POJ 3308 Paratroopers(最大流最小割の最小点权覆盖)

    Description It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the ...

  9. HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)

    Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “im ...

  10. js经典试题之ES6

    js经典试题之ES6 1:在ECMAScript6 中,Promise的状态 答案:pending  resolved(fulfilled) rejected 解析: Promise对象只有三种状态: ...