POJ 2018
又一水,设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的更多相关文章
- 【POJ 2018】 Best Cow Fences
[题目链接] http://poj.org/problem?id=2018 [算法] 二分平均值 检验时将每个数减去二分的值,求长度至少为L的子序列和的最大值,判断是否大于0 [代码] #includ ...
- POJ 2018 Best Cow Fences(二分答案)
题目链接:http://poj.org/problem?id=2018 题目给了一些农场,每个农场有一定数量的奶牛,农场依次排列,问选择至少连续排列F个农场的序列,使这些农场的奶牛平均数量最大,求最大 ...
- POJ - 2018 二分+单调子段和
依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...
- Best Cow Fences POJ - 2018 (二分)
Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains ...
- 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(二分最大区间平均数)题解
题意:给出长度>=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])$两点之间的斜率. 如果,我们目前在计算 ...
- 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 (二分答案构造新权值 or 斜率优化)
$ POJ~2018~Best~Cow~ Fences $(二分答案构造新权值) $ solution: $ 题目大意: 给定正整数数列 $ A $ ,求一个平均数最大的长度不小于 $ L $ 的子段 ...
随机推荐
- luogu2303 [SDOI2012] Longge的问题
题目大意:给出n,求sum foreach i(1<=i<=n) (gcd(n, i)). 1~n有太多的数,但是n与m的最大公约数却有很多重复.所以我们枚举最大公约数k,然后让k乘以与n ...
- CodeForces 651A(水题)
Friends are going to play console. They have two joysticks and only one charger for them. Initially ...
- 使用fastcgi部署django应用
1.fastcgi和cgi的区别 1)CGI (Common Gateway Interface): 用来作为 Web Server 同 Python, PHP 等的通信手段.而在静态网页的时代, 只 ...
- php gd
imagecopy() 函数用于拷贝图像或图像的一部分. imagecopyresized() 函数用于拷贝部分图像并调整大小. imagecopy() imagecopy() 函数用于拷贝图像或图像 ...
- vcpkg错误分析方法
最近在使用vcpkg时,经常会碰到CMake错误. 有些以前能编译通过的包, 过一段时间又不能编译错误了. 错误提示一般是CMake错误, 弄得很郁闷. 我采用以下步骤解决了问题: 分析错误 查看错误 ...
- How to solve a login problem of SQL Server?
Sometimes when we attempting to login the SQL Server 20xx Management Studio, when we type in the cor ...
- website robots.txt 防爬虫 措施
robots.txt文件用法举例: 1. 允许所有的robot访问 User-agent: * Allow: / 或者 User-agent: * Disallow: 2. 禁止所有搜索引擎访问网站的 ...
- java8-1-interface接口
Java 8 允许我们使用default关键字,为接口声明添加非抽象的方法实现.这个特性又被称为扩展方法 sample: interface Formula { double calculate(in ...
- Static variables in JavaScript
function MyClass () { // constructor function var privateVariable = "foo"; //NO:obj.privat ...
- JDBC+MYSQL初始学习
JDBC+MYSQL初始学习 一.学习准备 Eclipse 开发工具 + mysql数据库+navicat 数据库连接工具 Mysql的数据库连接驱动jar包 + testing测试集成+mave ...