Best Cow Fences

Time Limit: 1000MS Memory Limit: 30000K

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.

Output

* Line 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

Source

USACO 2003 March Green

/*
01分数规划思想+DP做法.
求长度不小于k连续一段的最大平均值.
ans=(sum[j]-sum[i-1])/(j-i+1) [j-i+1>=k]
这样的话我们二分一个ans,然后让每个数都减去ans.
最后DP检验最大的连续和是否大于0即可.
复杂度O(nlogn).
*/
#include<iostream>
#include<cstdio>
#define MAXN 100001
#define eps 1e-6
using namespace std;
double ans,s[MAXN],sum[MAXN],max1,min1=1e9;
int n,k;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
bool check(double x)
{
double tot,m=0;
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+s[i]-x;
for(int i=k;i<=n;i++)
{
tot=sum[i]-m;
if(tot>=0) return true;
m=min(m,sum[i-k+1]);
}
return false;
}
void erfen(double l,double r)
{
double mid;
while(l+eps<=r)
{
mid=(l+r)/2;
if(check(mid)) ans=mid,l=mid;
else r=mid;
}
int x=r*1000;
printf("%d\n",x);
return ;
}
int main()
{
int x;
while(~scanf("%d %d",&n,&k))
{
max1=0,min1=1e9;
for(int i=1;i<=n;i++)
{
s[i]=read();
sum[i]=sum[i-1]+s[i];
max1=max(max1,s[i]),min1=min(min1,s[i]);
}
erfen(min1,max1);
}
return 0;
}
/*
斜率优化题.
通过此题大概知道了斜率优化是什么.
因为ans=(sum[j]-sum[i-1])/(j-i+1) [j-i+1>=k].
所以我们可以将看做二维平面中的两个点(i-1,sum[i])和(j,sum[j]).
ans即为两点之间的斜率.
我们要让ans最大化.
n^2的暴力可以看做是有一个点t,和点集Gt{x,0<=x<=t-k},
扫描G中的每个点,使斜率最大化.
但是有些点对于决策是没有影响的.
现在有一个定理:
存在三个有序点i,j,k,如果满足k(i,j)>k(k,j)
即j点是直线ik上方的点,那它不会对决策产生影响.
证明的话见2004周源国家集训队论文,讲的很清楚.
这样的话我们维护一个下凸折线就好了.
我们每次都加入位置为i-k的点,维护下凸性.
如果某一点j(0<=j<=i-k)与点i的斜率最大,
则这个点显然一定是下凸折线的切点.
则它之前的点连线斜率较小也不会对决策产生影响
删掉就可以了.
这样的话由于每个点只入队出队一次
所以复杂度是O(n)的.
参考资料 2004周源国家集训队论文.
*/
#include<iostream>
#include<cstdio>
#define MAXN 100001
using namespace std;
double ans,sum[MAXN],max1,min1=1e9;
int n,k;
struct data{double x,y;}q[MAXN];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
double check(data a,data b,data c)
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
double Get(data a,data b)
{
return (b.y-a.y)/(b.x-a.x);
}
void slove()
{
data x,now;ans=0;
int head=0,tail=0;
for(int i=k;i<=n;i++)
{
x.x=i-k,x.y=sum[i-k];
while(tail-head>=2&&check(q[tail-2],q[tail-1],x)<0) tail--;//维护下凸性.
q[tail++]=x;
now.x=i,now.y=sum[i];
double k=Get(q[head],now);
while(tail-head>0&&Get(q[head+1],now)>k)//删除没用的点.
{
k=Get(q[head+1],now);
head++;
}
ans=max(ans,k);
}
ans*=1000;
int a=ans;
printf("%d\n",a);
}
int main()
{
int x;
while(~scanf("%d %d",&n,&k))
{
for(int i=1;i<=n;i++)
x=read(),sum[i]=sum[i-1]+x;
slove();
}
return 0;
}

Poj 2018 Best Cow Fences(分数规划+DP&&斜率优化)的更多相关文章

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

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

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

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

  3. BZOJ2402: 陶陶的难题II(树链剖分,0/1分数规划,斜率优化Dp)

    Description Input 第一行包含一个正整数N,表示树中结点的个数.第二行包含N个正实数,第i个数表示xi (1<=xi<=10^5).第三行包含N个正实数,第i个数表示yi ...

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

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

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

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

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

  7. 【BZOJ-4518】征途 DP + 斜率优化

    4518: [Sdoi2016]征途 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 230  Solved: 156[Submit][Status][ ...

  8. 【BZOJ-3437】小P的牧场 DP + 斜率优化

    3437: 小P的牧场 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 705  Solved: 404[Submit][Status][Discuss ...

  9. 【BZOJ-1010】玩具装箱toy DP + 斜率优化

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8432  Solved: 3338[Submit][St ...

随机推荐

  1. 如何在 WPF 中获取所有已经显式赋过值的依赖项属性

    原文:如何在 WPF 中获取所有已经显式赋过值的依赖项属性 获取 WPF 的依赖项属性的值时,会依照优先级去各个级别获取.这样,无论你什么时候去获取依赖项属性,都至少是有一个有效值的.有什么方法可以获 ...

  2. C#特性的学习(一)

    1.预定定义特性之一:AttributeUsage AttributeUsage有三个属性: 第一个属性:ValidOn 规定特性可被放置的语言元素,默认是AttributeTargets.All.

  3. SharePoint中用Power shell命令设置文档库列表的权限

    首先停止继承权限 $web = Get-PnPweb $spoList= Get-PnPList "Testlist" -Web $web (注释:获取对象)$spoList.Br ...

  4. Linux环境Ubuntu上安装GitLab

    本文主要介绍在Ubuntu[Ubuntu 18.04.3]上安装最新的GitLab版本控制工具. 一.安装更新GitLab所需要的依赖项 sudo apt-get update 下载过程中,网络要有所 ...

  5. NEST 多IndexType与分页

    /// <summary> /// POST /_all/employee/_search?typed_keys=true /// </summary> public void ...

  6. Ubuntu 18.04 LTS版本 谷歌拼音输入法安装

    为何安装? 自带IBUS框架对中文支持不稳定 采用对中文支持稳定的fcitx框架 如何安装? 步骤如下: 卸载自带IBUS框架    命令:sudo remove ibus 安装fcitx框架    ...

  7. 【转载】C#中string类使用Remove方法来移除指定位置的字符

    在C#的字符串操作过程中,有时候需要将字符串中指定位置的字符移除,此时就可能使用到字符串类string类中的Remove方法,此方法允许指定移除开始的开始的索引位置,以及移除的长度信息等,共有2个重载 ...

  8. 微信小程序组件通信入门及组件生命周期函数

    组件生命周期函数链接地址:https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/lifetimes.h ...

  9. 为新装的Centos 7X更换源,升级VIM失败,待解决

    CentOS 7X使用阿里云CentOS的yum源 1.备份原有repo文件 #cd /etc/yum.repos.d #mv /etc/yum.repos.d/CentOS-Base.repo /e ...

  10. 怎么对ZYNQ的FCLK做时钟组约束

    前言 对于包含PS和PL的设计,两者的数据交互PL必然会用到PS端的时钟. 对于FCLK(PS端时钟输入到PL端)的约束,此时钟的基础约束已在IP中产生.以下想约束其异步时钟的时钟组特性. 注意事项: ...