Best Cow Fences POJ - 2018 (二分)
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
Sample Input
10 6
6
4
2
10
3
8
5
9
4
1
Sample Output
6500 题意:n块田地,每块田地有cows【i】头牛,求出一个长度不小于F的子段,使子段牛的平均数最大。
思路:我们令 avr = sum【i,j】/(i-j+1)
那么这题就是 求是avr的最大值,我们二分枚举ans,判断 avr 是否不小于 ans,即avr >= ans,为了不维护(i-j+1)的值,变形成sum【i,j】 - ans*(i-j+1) >= 0,
我们把原数组cows【i】-ans,那么Sum【i,j】 = sum【i,j】-ans(i-j+1)。
现在关键就是取得一个 max{Sum【i,j】},对于Sum【i,j】,我们可以用前缀和相减的方式求得,sum(i)-min(sum(j)), 0 <= j <= i-F。 坑点:注意最后答应的是二分的R值,我打印L值Wa的怀疑人生.而且题目要求精度是1e-4,当我们枚举的精度不小于题目要求精度的时候L和R值都是OK的
我感觉是如果两个值转换为整数不同的话,R值转换的整数值在L~R区间内的,而l转换的值是小于l的,如果两个值转换的值相同,打印那个都行,且整数肯定小于L。 其实像是最大值最小,最小值最大,都可以用二分解决,答案是单调的,这题还有种用凸包方法写的,以后再填坑吧。
#include<cstdio>
#include<iostream>
using namespace std; const int maxn = 1e5+;
int n,f;
int cows[maxn];
const double eps = 1e-; bool solve(double x,int f)
{
double fcows[n+];
double sum[n+];
for(int i=;i<=n;i++)fcows[i] = cows[i] - x;
for(int i=;i<=n;i++)sum[i] = sum[i-]+fcows[i];
double ans = -1e10,minn = 1e10;
for(int i=f;i<=n;i++)
{
minn = min(minn,sum[i-f]);
ans = max(ans,sum[i]-minn);
}
return ans >= ;
}
int main()
{
scanf("%d%d",&n,&f);
double low=;
double high = ;
for(int i=;i<=n;i++)
{
scanf("%d",&cows[i]);
high += cows[i];
}
while(low + eps < high)
{
double mid = (low+high)/;
if(solve(mid,f))low = mid;
else high = mid;
}
printf("%d\n",(int)(*high));
}
Best Cow Fences POJ - 2018 (二分)的更多相关文章
- POJ - 2018 二分+单调子段和
依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...
- POJ 2018 Best Cow Fences(二分+最大连续子段和)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14601 Accepted: 4720 Desc ...
- Poj 2018 Best Cow Fences(分数规划+DP&&斜率优化)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Description Farmer John's farm consists of a ...
- 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 ...
- 一本通 1434:【例题2】Best Cow Fences
Best Cow Fences 二分答案 + 前缀和 个人认为题意没有表述清楚,本题要求的是满足题意的连续子序列(难度大大降低了有木有). 本题的精度也是非常令人陶醉,请您自行体会吧! #includ ...
- 1434:【例题2】Best Cow Fences
1434:[例题2]Best Cow Fences 时间限制: 1000 ms 内存限制: 65536 KB提交数: 263 通过数: 146 [题目描述] 给定一个长度为n的 ...
- 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(二分答案)
题目链接:http://poj.org/problem?id=2018 题目给了一些农场,每个农场有一定数量的奶牛,农场依次排列,问选择至少连续排列F个农场的序列,使这些农场的奶牛平均数量最大,求最大 ...
随机推荐
- python的学习笔记
1逻辑运算符不理解 2 在交互模式中,最后被输出的表达式结果被赋值给变量 _ .例如: >>> tax = 12.5 / 100 >>> price = 100.5 ...
- 《Oracle DBA工作笔记:运维、数据迁移与性能调优》 PDF 下载
一:下载途径 二:本书图样 三:本书目录 第1篇 数据库运维篇第1章 数据库安装配置1.1 安装前的准备 11.2 安装数据库软件 51.2.1 方法1:OUI安装 61.2.2 方法2:静默安装 8 ...
- Confluence 6 为登录失败编辑,禁用和配置验证码
在默认的情况下,验证码将会在失败登录次数达到的时候显示. 如果为登录失败编辑,禁用和配置验证码: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接. 在左侧 ...
- hdu4370 dijkstra矩阵转单向边最短路矩阵+自环闭环
/* 矩阵太神奇了Orz,网上的题解大多是spfa,不过我发想dijkstra也能做 把n*n的矩阵看成是单向边距离矩阵就行 */ #include<iostream> #include& ...
- 使用sysbench 0.5 对mysql 进行性能、压力测试
sysbench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况.目前sysbench代码托管在launchpad上,项目地址:https://launc ...
- c++ 链表基础功能实现
#include<stack> struct ListNode { int m_nValue; ListNode* m_pNext; }; ListNode* CreateListNode ...
- 学习Maven POM
什么是POM POM stands for "Project Object Model".It is an XML representation of a Maven projec ...
- B: Ocean的游戏(前缀和)
B: Ocean的游戏 Time Limit: 1 s Memory Limit: 128 MB Submit My Status Problem Description 给定一个字符串s, ...
- MySQL查看库表的大小
MySQL数据库空间使用情况查询 如果想知道MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表, ...
- 【C++ Primer | 07】泛型算法
定制操作 #include <iostream> #include <string> #include <vector> #include <algorith ...