Best Cow Fences
题目描述
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.
输入
* 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.
输出
样例输入
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的更多相关文章
- 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 Total Submissions: 10174 Accepted: 3294 Desc ...
- POJ2018 Best Cow Fences —— 斜率优化DP
题目链接:https://vjudge.net/problem/POJ-2018 Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K T ...
- 一本通 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的 ...
- loj#10012\poj2018 Best Cow Fences(二分)
题目 #10012 「一本通 1.2 例 2」Best Cow Fences 解析 有序列\(\{a_i\}\),设\([l,r]\)上的平均值为\(\bar{x}\),有\(\sum_{i=l}^r ...
- 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 (二分答案构造新权值 or 斜率优化)
$ POJ~2018~Best~Cow~ Fences $(二分答案构造新权值) $ solution: $ 题目大意: 给定正整数数列 $ A $ ,求一个平均数最大的长度不小于 $ L $ 的子段 ...
- [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 ...
随机推荐
- 【小程序入门集锦】19,微信小程序个人帐号申请
个人账号与企业帐号相比,缺少支付等功能,与个人订阅号类似. 小程序开放个人开发者申请注册,个人用户可访问微信公众平台,扫码验证个人身份后即可完成小程序帐号申请并进行代码开发. 下面我们就来说说 ...
- 【Luogu P3371&P4779】【模板】单源最短路径(线段树优化Dijkstra)
线段树优化$\rm dijkstra$ 线段树每个节点维护$[l,r]$中$dist$最小的点,删除则把该点$dist$赋值为$+\infty$,然后更新该点影响到的线段树上的其他节点即可. 可以得到 ...
- IOS开发学习笔记043-QQ聊天界面实现
QQ聊天界面实现 效果如下: 实现过程: 1.首先实现基本界面 头像使用 UIImageView : 文字消息使用 UIButton 标签使用 UILable :水平居中 所有元素在一个cell中,在 ...
- Python-S9-Day115——Flask Web框架基础
01 今日内容概要 02 内容回顾 03 Flask框架:配置文件导入原理 04 Flask框架:配置文件使用 05 Flask框架:路由系统 06 Flask框架:请求和响应相关 07 示例:学生管 ...
- hnust 可口可乐大促销
问题 B: 可口可乐大促销 时间限制: 1 Sec 内存限制: 128 MB提交: 653 解决: 323[提交][状态][讨论版] 题目描述 最近可口可乐在搞大促销活动,1瓶可乐只要1元钱.而且 ...
- Leetcode 600.不包含连续1的非负整数
不包含连续1的非负整数 给定一个正整数 n,找出小于或等于 n 的非负整数中,其二进制表示不包含 连续的1 的个数. 示例 1: 输入: 5 输出: 5 解释: 下面是带有相应二进制表示的非负整数&l ...
- android AsyncTask使用限制
由于AsyncTask内部是使用线程池(ThreadPoolExecutor)来管理要处理的任务的,所以AsyncTask的弊端就非常明确了:要extcute的任务数量超过线程池最大容量时,必然会报错 ...
- DownloadManager的使用
DownloadManager是系统开放给第三方应用使用的类,包含两个静态内部类DownloadManager.Query和DownloadManager.Request.DownloadManage ...
- MFC录制音频和播放音频
一.录制音频 在windows中提供了相应的API函数(waveIn这个族的函数)实现录音功能:在使用这些函数时,一定要引入相应的头文件 #include <windows.h> #inc ...
- vs编译生成之后报错
严重性 代码 说明 项目 文件行 禁止显示状态 错误 CS2001 Source file 'D:\Local\Apright_LW-Wiseb2b\Feekong.Model\obj\Release ...