斜率优化。

设$s[i]$表示前缀和,$avg(i,j)=(s[j]-s[i-1])/(j-(i-1))$。就是$(j,s[j])$与$(i-1,s[i-1])$两点之间的斜率。

如果,我们目前在计算$px$与哪个点相连斜率最大,那么一定不会是$pj$点。因为不是$pi$比优就是$pk$比$pi$优。因此斜率优化可以将$pj$这样的点直接删除。

因此队列里存着的相邻两点的斜率是不断增大的。

因为$(s[i+1]-s[i])/1$是大于等于$1$的,所以如果$pi$点与$pt$点形成的斜率最大,那么与$pi+1$点形成最大斜率的点肯定大于等于$pt$点。

根据这一点,每一个点入队一次出队一次,时间复杂度$O(n)$。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} int n,k;
double a[],s[];
int q[],f1,f2; bool delete1(int a,int b,int c)
{
if((s[c]-s[a])*(c-b)<(s[c]-s[b])*(c-a)) return ;
return ;
} bool delete2(int a,int b,int c)
{
if((s[c]-s[b])*(b-a)<(s[b]-s[a])*(c-b)) return ;
return ;
} int main()
{
while(~scanf("%d%d",&n,&k))
{
for(int i=;i<=n;i++) scanf("%lf",&a[i]);
for(int i=;i<=n;i++) s[i]=s[i-]+a[i]; f1=; f2=; q[f2]=; double ans=;
for(int i=k;i<=n;i++)
{
while()
{
if(f2-f1+<) break;
if(delete1(q[f1],q[f1+],i)) f1++;
else break;
} ans=max(ans,1.0*(s[i]-s[q[f1]])/(i-q[f1])); while()
{
if(f2-f1+<) break;
if(delete2(q[f2-],q[f2],i-k+)) f2--;
else break;
} f2++; q[f2]=i-k+; }
printf("%d\n",(int)(*ans));
}
return ;
}

POJ 2018 Best Cow Fences的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. POJ 3617 Best Cow Line(最佳奶牛队伍)

    POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...

  7. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

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

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

  9. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

随机推荐

  1. AtCoder Grand Contest 028 B - Removing Blocks 解题报告

    B - Removing Blocks Time limit : 2sec / Memory limit : 1024MB Score : 600 points ## Problem Statemen ...

  2. picks loves segment tree I

    picks loves segment tree I 题目背景 来源: \(\text {2018 WC Segment Tree Beats}\) 原作者: \(\text {C_SUNSHINE} ...

  3. 部分经典IT书籍

    部分经典IT书籍 [系统,网路管理]1) Learning the Unix Operating System 1565923901 O'reilly/1997-4ed ***强力推荐给想入门unix ...

  4. 【BZOJ 4514】[Sdoi2016]数字配对 费用流

    利用spfa流的性质,我直接拆两半,正解分奇偶(妙),而且判断是否整除且质数我用的是暴力根号,整洁判断质数个数差一(其他非spfa流怎么做?) #include <cstdio> #inc ...

  5. nodejs npm insttall 带不带-g这个参数的区别

    -g 中的g是global的意思所以带-g这个参数是全局安装,不带-g这个参数是本地安装. 在windows系统中全局安装的目录在:C:\Users\linsenq\AppData\Roaming\n ...

  6. xiaoluo同志Linux学习之CentOS6.4

    小罗同志写的不错,弄个列表过来啊   Linux学习之CentOS(三十六)--FTP服务原理及vsfptd的安装.配置 xiaoluo501395377 2013-06-09 01:04 阅读:56 ...

  7. es6快速排序

    let qsort = fn =>([x,...xn]) => x == null ? [] : [ ...qsort(fn)(xn.filter(a=>fn(a,x))), x, ...

  8. The 'brew link' step did not complete successfully

    在mac 上更新node时遇到了一系列的问题: 卸载node重新安装之后提示: The 'brew link' step did not complete successfully 其实这里已经给出了 ...

  9. [洛谷P1040] 加分二叉树

    洛谷题目链接:加分二叉树 题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,-,n),其中数字1,2,3,-,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di ...

  10. StringUtils工具类的使用

    注意isNotEmpty()和isNotBlank()的区别 过去的字符串判空:if("".equals(str) || str == null).现在使用工具类StringUti ...