题目描述

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. 

输入

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

输出

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

样例输入

10 6
6
4
2
10
3
8
5
9
4
1

样例输出

6500

分析:据说可以斜率DP,看别人题解的时候觉得很有道理,但是就是WA可还行。后来被逼无奈二分答案+DP了。

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#define range(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define rerange(i,a,b) for(int i=a;i>=b;--i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
int n,f;
double ss[],aa[],Left=0x7fffffff,Right;
void init(){
cin>>n>>f;
range(i,,n){
cin>>aa[i];
ss[i]+=ss[i-]+aa[i];
Left=min(Left,aa[i]);
Right=max(Right,aa[i]);
}
}
bool judge(double val){
double tmp,pre=ss[f-]-(f-)*val;
range(i,f,n){
tmp=(ss[i]-ss[i-f])-f*val;
pre+=aa[i]-val;
pre=max(tmp,pre);
if(pre>-1e-)return true;
}
return false;
}
void solve(){
while(Right-Left>1e-){
double mid=(Right+Left)/;
if(judge(mid))Left=mid;
else Right=mid;
}
cout<<(int)(Right*)<<endl;
}
int main() {
init();
solve();
return ;
}

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 Total Submissions: 10174 Accepted: 3294 Desc ...

  3. POJ2018 Best Cow Fences —— 斜率优化DP

    题目链接:https://vjudge.net/problem/POJ-2018 Best Cow Fences Time Limit: 1000MS   Memory Limit: 30000K T ...

  4. 一本通 1434:【例题2】Best Cow Fences

    Best Cow Fences 二分答案 + 前缀和 个人认为题意没有表述清楚,本题要求的是满足题意的连续子序列(难度大大降低了有木有). 本题的精度也是非常令人陶醉,请您自行体会吧! #includ ...

  5. 1434:【例题2】Best Cow Fences

    1434:[例题2]Best Cow Fences 时间限制: 1000 ms         内存限制: 65536 KB提交数: 263     通过数: 146 [题目描述] 给定一个长度为n的 ...

  6. loj#10012\poj2018 Best Cow Fences(二分)

    题目 #10012 「一本通 1.2 例 2」Best Cow Fences 解析 有序列\(\{a_i\}\),设\([l,r]\)上的平均值为\(\bar{x}\),有\(\sum_{i=l}^r ...

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

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

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

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

  9. [USACO2003][poj2018]Best Cow Fences(数形结合+单调队列维护)

    http://poj.org/problem?id=2018 此乃神题……详见04年集训队论文周源的,看了这个对斜率优化dp的理解也会好些. 分析: 我们要求的是{S[j]-s[i-1]}/{j-(i ...

  10. Poj2018 Best Cow Fences

    传送门 题目大意就是给定一个长度为 n 的正整数序列 A ,求一个平均数最大的,长度不小于 L 的子序列. 思路: 二分答案. Code: #include<iostream> #incl ...

随机推荐

  1. Centos7和Centos6防火墙开放端口配置方法(避坑教学)

    ▲这篇文章主要为大家详细介绍了Centos7防火墙开放端口的快速方法,感兴趣的小伙伴们可以参考一下! 一.CentOS 7快速开放端口: CentOS升级到7之后,发现无法使用iptables控制Li ...

  2. 设计模式之第18章-观察者模式(Java实现)

    设计模式之第18章-观察者模式(Java实现) 话说曾小贤,也就是陈赫这些天有些火,那么这些明星最怕的,同样最喜欢的是什么呢?没错,就是狗仔队.英文的名字比较有意思,是paparazzo,这一说法据说 ...

  3. 11、JQuery知识点总结

    1.JQuery简介 JQuery 是一套跨浏览器的JavaScript库,简化HTML与JavaScript之间的操作 jQuery有下列特色: 跨浏览器的DOM元素选择 DOM巡访与更改:支持CS ...

  4. Python-S9—Day85-ORM项目实战之forms组件以及Modelform补充、跨域请求及应用

    01 forms组件补充1 02 forms组件补充2 03 ModelForm回顾 04 浏览器的历史 05 jsonop实现跨域请求 06 jsonop实现跨域请求2 07 jsonop实现跨域请 ...

  5. python IDLE简介及使用技巧

    前言:本人环境windows 7 64位,python2.7 IDLE简介: 是python 的可视化GUI编辑器 可以逐行输入命令 可方便的进行复制.粘贴等操作 常用命令行命令: import mo ...

  6. 关于C++ STL标准库中map 的多元素应用

    map的特性是,所有的元素会根据键值自动排序.map的所有元素都是pair,同时拥有实值(value)和键值(key).pair的第一个元素被视为键值,第二个被视为实质piar 的定义 templat ...

  7. sqlserver 获取一个月有多少天

    --思路:给定日期的下一个月的1号减去1天,然后取datepart(DAY,dt) declare @dt varchar(10)select @dt='2013-11-20'select datep ...

  8. home.php

    home.php <?php error_reporting(0); //抑制所有错误信息 @header("content-Type: text/html; charset=utf- ...

  9. Codeforces #990E Post Lamp

    题目大意 今欲用若干条长为 $k$($1\le k\le m, k\in \mathbb{Z}$) 的线段覆盖数轴上 $[0,n]$ 这一段.线段的起点(左端点)必须为 $[0, n-1]$ 中的某个 ...

  10. [HEOI2016/TJOI2016][bzoj4555] 求和 [斯特林数+NTT]

    题面 传送门 思路 首先,我们发现这个式子中大部分的项都和$j$有关(尤其是后面的$2^j\ast j!$),所以我们更换一下枚举方式,把这道题的枚举方式变成先$j$再$i$ $f(n)=\sum_{ ...