poj2018——Best Cow Fences
Description
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
* 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
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
题意:
给定一个正整数序列a,求一个平均数最大的、长度不小于L的子段。
Solution:
本题由于是求平均数的最大值,我们很容易往二分方向上想,二分答案的关键是如何取check,判定“是否存在一个长度不小与L的子段平均数不小于二分的值”。
考虑这样一种思路,把数列中每个数都减去二分的值,就转化为判断是否存在一个长不小于L的子段,子段和非负。继续思考,
1、若没有L的限制,只需O(n)扫描数列不断加数当和为负数时就把当前子段清空,扫描过程中出现过的最大子段和即为所求。
2、求一个子段它的和最大且长度不小与L。子段和可以转化为前缀和相减的形式,即设sumi表示ai~aj的和,则:
仔细观察上面的式子容易发现,随着i的增长,j的取值范围0~i-L每次只会增大1。换言之,每次只会有一个新的取值进入min{sumj}的候选集合,所以我们没有必要每次循环枚举j,只需要用一个变量记录当前的最小值,每次与新的取值sumi-L取min就可以了。
于是我们只需要看一下最大子段和是不是非负数就可以确定二分上下界的变化范围了。
(没看懂没事,第一遍我也没看懂,仔细看并对照代码,f**k真的简单巧妙!)
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#define il inline
#define ll long long
#define debug printf("%d %s\n",__LINE__,__FUNCTION__)
using namespace std;
il int gi()
{
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-')x=getchar();
if(x=='-')x=getchar(),f=;
while(x>=''&&x<='')a=a*+x-,x=getchar();
return f?-a:a;
}
const double eps=1e-;
int n,L;
double a[],b[],sum[];
int main()
{
n=gi(),L=gi();
for(int i=;i<=n;i++)a[i]=gi();
double l=-1e6,r=1e6;
while(r-l>eps){
double mid=(l+r)/,ans=-1e10,minn=1e10;
for(int i=;i<=n;i++)b[i]=a[i]-mid;
for(int i=;i<=n;i++)sum[i]=sum[i-]+b[i];
for(int i=L;i<=n;i++)minn=minn<sum[i-L]?minn:sum[i-L],ans=max(ans,sum[i]-minn);
if(ans>=)l=mid;else r=mid;
}
printf("%d\n",int(r*));
return ;
}
poj2018——Best Cow Fences的更多相关文章
- POJ2018 Best Cow Fences —— 斜率优化DP
题目链接:https://vjudge.net/problem/POJ-2018 Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K T ...
- POJ-2018 Best Cow Fences(二分加DP)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10174 Accepted: 3294 Desc ...
- loj#10012\poj2018 Best Cow Fences(二分)
题目 #10012 「一本通 1.2 例 2」Best Cow Fences 解析 有序列\(\{a_i\}\),设\([l,r]\)上的平均值为\(\bar{x}\),有\(\sum_{i=l}^r ...
- [USACO2003][poj2018]Best Cow Fences(数形结合+单调队列维护)
http://poj.org/problem?id=2018 此乃神题……详见04年集训队论文周源的,看了这个对斜率优化dp的理解也会好些. 分析: 我们要求的是{S[j]-s[i-1]}/{j-(i ...
- Poj2018 Best Cow Fences
传送门 题目大意就是给定一个长度为 n 的正整数序列 A ,求一个平均数最大的,长度不小于 L 的子序列. 思路: 二分答案. Code: #include<iostream> #incl ...
- POJ2018 Best Cow Fences 二分
实数折磨人啊啊啊啊啊啊啊 好,实数应该是最反人类的东西了...... 这个害得我调了0.5天才过. 大意是这样的:给你一个数列,求其中不少于f个的连续数的最大平均值. 不禁想起寒假的课程来... 此处 ...
- POJ-2018 Best Cow Fences 二分
题意:找到一个连续区间,区间的长度至少大于f,现在要求这个区间的平均值最大. 题解: 二分找答案. 每次对于2分的mid值, 都把原来的区间减去mid, 然后找到一长度至少为f的区间, 他们的区间和& ...
- poj2018 Best Cow Fences[二分答案or凸包优化]
题目. 首先暴力很好搞,但是优化的话就不会了.放弃QWQ. 做法1:二分答案 然后发现平均值是$ave=\frac{sum}{len}$,这种形式似乎可以二分答案?把$len$移到左边. 于是二分$a ...
- POJ 2018 Best Cow Fences(二分+最大连续子段和)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14601 Accepted: 4720 Desc ...
随机推荐
- 阅读笔记《JavaScript高级程序设计》
0. 严格模式 "user strict" (1整个脚本顶部,2函数体顶部) 1. 数据类型 undefined -- 未定义 boolean string number obje ...
- android 学习四 ContentProvider
1.系统自带的许多数据(联系人,本地信息等)保存在sqllite数据库,然后封装成许多ContentProvider来供其他程序访问. 2.对sqllite数据库的操作,可以在命令行通过adb工具登录 ...
- http性能测试点滴
WeTest 导读 在服务上线之前,性能测试必不可少.本文主要介绍性能测试的流程,需要关注的指标,性能测试工具apache bench的使用,以及常见的坑. 什么是性能测试 性能测试是通过自动化的测试 ...
- 阿里云ECS下Ubuntu 16.04系统安装python3.6.5 环境并设置为默认
一.添加python3.6安装包并安装: 二.修改系统默认python版本为3.6: 三.安装并升级pip版本: 一.添加python3.6安装包并安装: sudo apt-get install s ...
- C 计算身高
#include <stdio.h> int main(int argc, char **argv) { //输出一句话printf("请分别输入身高的英尺和英寸," ...
- 【swiper】 滑块组件说明
swiper 滑块视图容器,其原型如下: <swiper indicator-dots="[Boolean]" indicator-color="[Color]&q ...
- 搜索二维矩阵 II
描述 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复的整数. 样例 ...
- ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《二》
ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象<一> 上一篇文章写到对象之间传输使用线程方式 ,无法使用监听方式,最近解决了使用监听方式接收对象,本次使用配置文件方 ...
- leetcode个人题解——#18 4sums
在3sums的基础上加了一层循环. class Solution { public: vector<vector<int>> fourSum(vector<int> ...
- 使用truffle测试部署合约
truffle console let contract; contract=BloggerCoin.deployed().then(instance=>contract=instanc e);