POJ-2018 Best Cow Fences(二分加DP)
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.
OutputLine 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)的更多相关文章
- Poj 2018 Best Cow Fences(分数规划+DP&&斜率优化)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Description Farmer John's farm consists of a ...
- POJ 2018 Best Cow Fences(二分+最大连续子段和)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14601 Accepted: 4720 Desc ...
- POJ 2018 Best Cow Fences (二分答案构造新权值 or 斜率优化)
$ POJ~2018~Best~Cow~ Fences $(二分答案构造新权值) $ solution: $ 题目大意: 给定正整数数列 $ A $ ,求一个平均数最大的长度不小于 $ L $ 的子段 ...
- POJ 2018 Best Cow Fences(二分答案)
题目链接:http://poj.org/problem?id=2018 题目给了一些农场,每个农场有一定数量的奶牛,农场依次排列,问选择至少连续排列F个农场的序列,使这些农场的奶牛平均数量最大,求最大 ...
- POJ 2018 Best Cow Fences(二分最大区间平均数)题解
题意:给出长度>=f的最大连续区间平均数 思路:二分这个平均数,然后O(n)判断是否可行,再调整l,r.判断方法是,先求出每个数对这个平均数的贡献,再求出长度>=f的最大贡献的区间,如果这 ...
- 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])$两点之间的斜率. 如果,我们目前在计算 ...
- POJ2018 Best Cow Fences —— 斜率优化DP
题目链接:https://vjudge.net/problem/POJ-2018 Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K T ...
- loj#10012\poj2018 Best Cow Fences(二分)
题目 #10012 「一本通 1.2 例 2」Best Cow Fences 解析 有序列\(\{a_i\}\),设\([l,r]\)上的平均值为\(\bar{x}\),有\(\sum_{i=l}^r ...
- poj2018 Best Cow Fences[二分答案or凸包优化]
题目. 首先暴力很好搞,但是优化的话就不会了.放弃QWQ. 做法1:二分答案 然后发现平均值是$ave=\frac{sum}{len}$,这种形式似乎可以二分答案?把$len$移到左边. 于是二分$a ...
随机推荐
- 5 -- Hibernate的基本用法 --4 5 JNDI数据源的连接属性
如果无须Hibernate自己管理数据源,而是直接访问容器管理数据源,Hibernate可使用JNDI(Java Naming Directory Interface,Java命名目录接口)数据源的相 ...
- Eclipse cdt解决github导入的项目无法打开声明的bug (cannot open declaration)
概述: 我利用eclipse 的git插件clone github上的远程项目(C++)到本地时遇到一个问题:clone下来的项目没有C++特性,无法使用open declaration等操作,下面是 ...
- NetBpm 目录
整理了一下网上的一些netbpm,虽然这项目现在不再更新了,还是想去学习一下,这个组件用时候很方便,比workFlow方便的多了 如果像jbpm那样一直更新就好了. 前两篇是个人的一个总结,后面一些是 ...
- html+jquery制作网页地图
http://jvectormap.com/ <!--StartFragment --> JVectorMap 是一个显示矢量地图的jQuery插件.它使用 SVG 在Firefox 3 ...
- windows本地hash值获取和破解详解
powershell版的procdump https://www.t00ls.net/articles-48428.html procdump procdump是微软官方提供的一个小工具, 微软官方下 ...
- 【RF库XML测试】测试的XML文件说明
文件存放路径:C:\workspace\robotframework\test_rf_api\testdata\XML.xml 文件内容: <example> <first id=& ...
- mysql类型对应Java的类型
整型 JDBCtinyint java.lang.Integersmallintmediumint java.lang.Longint bigint ...
- react中的hoc和修饰器@connect结合使用
在学习react-redux的时候,看到了修饰器这个新的属性,这个是es7的提案属性,很方便.于是我用@connect代替了connect(使用的时候需要配置,这里不赘述),省去了很多不必要的代码,但 ...
- springmvc接收前台(可以是ajax)传来的数组list,map,set等集合,复杂对象集合等图文详解
参考帖子: http://blog.csdn.net/wabiaozia/article/details/50803581 方法参考: { "token":"" ...
- linux批量修改文件名
源文件; [root@test_machine fuzj]# ls fuzj-1.txt fuzj-2.txt fuzj-3.txt fuzj-4.txt fuzj-5.txt fuzj-6 ...