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 ...
随机推荐
- 一份比较全面的PHP开发编码规范.
这些年来多从事Linux下PHP和C相关的开发,带过很多项目和团队,下面是根据经验整理的PHP编码规范,可以用作给大家的范例和参考,根据需要进行取舍和修改! (可能最新的一些php5的规范不够完整,今 ...
- Dubbo原码解析(version:2.5.3)
一.启动dubbo借助spring的schemas来启动(dubbo.jar/META-INF/spring.schemas).在dubbo.jar/META-INF/spring.handlers里 ...
- 【Oracle】BLOB
1.データベースからの読み込み Dim strSql As String = "select IMG from TBL where ID=XX" Dim cmd As New Or ...
- SpringMVC由浅入深day01_5注解的处理器映射器和适配器
5 注解的处理器映射器和适配器 在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandle ...
- SpringMVC -- 梗概--源码--贰--拦截器:Interceptor
附:实体类 1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versi ...
- CentOS7--使用yum安装和管理软件
yum是红帽软件包管理器,它能能够查询,安装和卸载软件包,以及将整个系统更新到最新的可用版本.Yum可以在安装的过程中自动解决依赖关系. 1. 检查和更新软件包 1.1 查询更新 查看系统上哪些已安装 ...
- Android开发懒人库 -- ButterKnife (转载)
ButterKnife:8.1.0的使用 http://www.jianshu.com/p/0392199a682b http://www.cnblogs.com/flyme/p/4517560. ...
- JSP中的动态包含和静态包含的区别
本文转载自http://blog.csdn.net/xuxu198899223/article/details/8501044 1. 语法格式 (1)静态包含:<%@ include file= ...
- Material Design系列第五篇——Working with Drawables
Working with Drawables This lesson teaches you to Tint Drawable Resources Extract Prominent Colors f ...
- python基础---->python的使用(六)
这里记录一下python中关于class类的一些知识.不解释就弄不懂的事,就意味着怎样解释也弄不懂. python中的类知识 一.class的属性引用与实例 class MyClass(): '''A ...