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

【题意】给出n个数,在这n个数里面找到一些连续的数,这些数的数量大于等于m,并且他们的平均值在这n个数里面最大

【思路】先把n个数的最大最小值确定,然后二分枚举平均值,对于每一个连续数,只要他们减去平均值大于0,就调制上限制,不然调整下限制,.......

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=;
double a[N],sum[N];
int n,m;
int check(double k)
{
double tmp=sum[m-]-(m-)*k;
for(int i=m;i<=n;i++)
{
tmp+=a[i]-k;
tmp=max(tmp,sum[i]-sum[i-m]-m*k);//新选m个数和前面的数比较,取最大值
if(tmp>-1e-)
return ;
}
return ;
}
int main()
{ while(~scanf("%d%d",&n,&m))
{
sum[]=;
double maxn=,minx=inf;
for(int i=;i<=n;i++)
{
scanf("%lf",&a[i]);
if(a[i]>maxn)
maxn=a[i];
if(a[i]<minx)
minx=a[i];
sum[i]=sum[i-]+a[i];
}
while(maxn-minx>=1e-)
{
double mid=(maxn+minx)/;
if(check(mid))
minx=mid;
else maxn=mid;
}
int ans=*maxn;
printf("%d\n",ans);
}
return ;
}

Best Cow Fences_二分&&DP的更多相关文章

  1. 二分+DP HDU 3433 A Task Process

    HDU 3433 A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  2. hdu 3433 A Task Process 二分+dp

    A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. 2018.10.24 NOIP模拟 小 C 的数组(二分+dp)

    传送门 考试自己yyyyyy的乱搞的没过大样例二分+dp二分+dp二分+dp过了606060把我自己都吓到了! 这么说来乱搞跟被卡常的正解比只少101010分? 那我考场不打其他暴力想正解血亏啊. 正 ...

  4. 「学习笔记」wqs二分/dp凸优化

    [学习笔记]wqs二分/DP凸优化 从一个经典问题谈起: 有一个长度为 \(n\) 的序列 \(a\),要求找出恰好 \(k\) 个不相交的连续子序列,使得这 \(k\) 个序列的和最大 \(1 \l ...

  5. 【bzoj1044】[HAOI2008]木棍分割 二分+dp

    题目描述 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长度最小, 并且 ...

  6. Luogu P2511 [HAOI2008]木棍分割 二分+DP

    思路:二分+DP 提交:3次 错因:二分写萎了,$cnt$记录段数但没有初始化成$1$,$m$切的次数没有$+1$ 思路: 先二分答案,不提: 然后有个很$naive$的$DP$: 设$f[i][j] ...

  7. 洛谷$P4322\ [JSOI2016]$最佳团体 二分+$dp$

    正解:二分+$dp$ 解题报告: 传送门$QwQ$ 这题长得好套路嗷,,,就一看就看出来是个$01$分数规划+树形$dp$嘛$QwQ$. 考虑现在二分的值为$mid$,若$mid\leq as$,则有 ...

  8. $bzoj3872\ [Poi2014]\ Ant\ colony$ 二分+$dp$

    正解:二分+$dp$ 解题报告: 传送门$QwQ$ 一年过去了依然没有头绪,,,$gql$的$NOIp$必将惨败了$kk$. 考虑倒推,因为知道知道除数和答案,所以可以推出被除数的范围,然后一路推到叶 ...

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

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

随机推荐

  1. 初学java之(盒子分布)

    import javax.swing.*; import java.awt.*; class WinGrid extends JFrame { Box basebox , boxv1,boxv2; p ...

  2. LDM和STM指令

    LDM批量加载/STM批量存储指令可以实现一组寄存器和一块连续的内存单元之间传输数据. 允许一条指令传送16个寄存器的任意子集和所有寄存器,指令格式如下: LDM{cond}  mode  Rn{!} ...

  3. script "text/template"

    <script type="text/template" id="orgItem"> <div class="{orgClass}& ...

  4. hdu5876 Sparse Graph(补图最短路 bfs)

    题目链接:hdu5876 Sparse Graph 详见代码.. #include<cstdio> #include<cstring> #include<algorith ...

  5. css改变图片的颜色

    参考大神张鑫旭:http://www.zhangxinxu.com/wordpress/2016/06/png-icon-change-color-by-css/ 主要知识点:border-right ...

  6. <context:annotation-config> 和 <context:component-scan>的差别

    <context:annotation-config> is used to activate annotations in beans already registered in the ...

  7. AlarmManager手机闹钟简介

    1.void set(int type , long triggerAtTime , PendingIntent operation ) : 设置在 triggerAtTime时间启动由operati ...

  8. MySql 分组排序取时间最大的一条记录

    SELECT A.* FROM digital_asset A, (SELECT name, max(last_updated) max_day FROM digital_asset GROUP BY ...

  9. Oracle异常处理内容,隐式游标

    异常处理 create or replace procedure pr_test3(v_bh in varchar2,v_xx out t_hq_ryxx%rowtype) is begin sele ...

  10. bzoj 2243: [SDOI2011]染色

    #include<cstdio> #include<iostream> #define M 1000006 #define N 1000006 using namespace ...