又一水,设dp[i]为以i结尾的有最大平均值的起始位置。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string.h>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
int cow[100010];
int dp[100010]; int n,f; int main(){
while(scanf("%d%d",&n,&f)!=EOF){
cow[0]=0;
for(int i=1;i<=n;i++){
scanf("%d",&cow[i]);
cow[i]+=cow[i-1];
}
dp[f]=1;
int pos=f;
double ans1,ans2,ans3;
for(int i=f+1;i<=n;i++){
ans1=(cow[i]-cow[dp[i-1]-1])*1.0/(i-dp[i-1]+1);
ans2=(cow[i]-cow[i-f])*1.0/(f);
if(ans1>ans2){
dp[i]=dp[i-1];
}
else dp[i]=i-f+1;
ans3=(cow[pos]-cow[dp[pos]-1])*1.0/(pos-dp[pos]+1);
ans1=(cow[i]-cow[dp[i]-1])*1.0/(i-dp[i]+1);
if(ans1>ans3)
pos=i;
}
printf("%d\n",int((cow[pos]-cow[dp[pos]-1])*1000.0/(pos-dp[pos]+1)));
}
return 0;
}

  

POJ 2018的更多相关文章

  1. 【POJ 2018】 Best Cow Fences

    [题目链接] http://poj.org/problem?id=2018 [算法] 二分平均值 检验时将每个数减去二分的值,求长度至少为L的子序列和的最大值,判断是否大于0 [代码] #includ ...

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

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

  3. POJ - 2018 二分+单调子段和

    依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...

  4. Best Cow Fences POJ - 2018 (二分)

    Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains ...

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

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

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

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

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

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

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

  9. POJ 2018 Best Cow Fences (二分答案构造新权值 or 斜率优化)

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

随机推荐

  1. Referenced file contains errors (http://www.springframework.org/schema/beans/spring-beans-3.0.xsd)

    问题: java项目在Eclipse中xml有小红叉 Problems:Referenced file contains errors (http://www.springframework.org/ ...

  2. poj1700--贪心--Crossing River

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12260   Accepted: 4641 D ...

  3. PDOHelper (原创)

    class PDOHelper{ public static $db =null;// new PDO('mysql:host=192.168.1.68;dbname=test','root','12 ...

  4. BZOJ 1018 线段树维护图的连通性问题

    思路: 我们可以搞一棵线段树 对于一段区间有6种情况需要讨论 左上右下.左上右上.左下右下.左下右上 这四种比较好维护 用左上右下举个例子吧 就是左儿子的左上右下&左区间到右区间下面有路&am ...

  5. Docker 配置与实践清单

    https://mp.weixin.qq.com/s/yeEkF5DKa9IjmIvuzOTT3g

  6. ridis 集群配置

    ./redis-cli -h 192.168.106.128 -p  6379 redis 1.ping 2.set str1 abc    get str1 3.  mkdir ../redis-c ...

  7. js-字符串方法

    字符串 遍历字符串 方法:(类似数组) 使用for 或 for… in      结果:得到字符串中的每个字符 查找字符 ²  charAt(索引值) 注: 超出索引值范围时,则返回空字符 ²  ch ...

  8. Android WindowManager和WindowManager.LayoutParams的使用以及实现悬浮窗口的方法

    1.理清概念 我们使用过Dialog和PopupWindow,还有Toast,它们都显示在Activity之上.那么我们首先需要理解的是android中是如何去绘制这些UI的呢?这里我只讲我所理解的, ...

  9. sql server restore DB issue

    error occurs when restoring the backup file of sql server(DB.bak) to run the above t-sql will shoot ...

  10. 创建一个dynamics CRM workflow (三) - Creating Configuration Entity for Custom Workflow

    上个帖子中, 我们创建了个发email的workflow. 但是我们邮件当中的tax 值是 hard code, 这在开发当中是不容许的. 那今天我们来把这个build in workflow用 in ...