博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二分答案 [Usaco2014 Mar]Sabotage
阅读量:4487 次
发布时间:2019-06-08

本文共 2384 字,大约阅读时间需要 7 分钟。

问题 L: [Usaco2014 Mar]Sabotage

时间限制: 1 Sec 内存限制: 128 MB
题目描述
Farmer John”s arch-nemesis, Farmer Paul, has decided to sabotage Farmer John”s milking equipment! The milking equipment consists of a row of N (3 <= N <= 100,000) milking machines, where the ith machine produces M_i units of milk (1 <= M_i <= 10,000). Farmer Paul plans to disconnect a contiguous block of these machines – from the ith machine up to the jth machine (2 <= i <= j <= N-1); note that Farmer Paul does not want to disconnect either the first or the last machine, since this will make his plot too easy to discover. Farmer Paul”s goal is to minimize the average milk production of the remaining machines. Farmer Paul plans to remove at least 1 cow, even if it would be better for him to avoid sabotage entirely. Fortunately, Farmer John has learned of Farmer Paul”s evil plot, and he is wondering how bad his milk production will suffer if the plot succeeds. Please help Farmer John figure out the minimum average milk production of the remaining machines if Farmer Paul does succeed.

约翰的牧场里有 N 台机器,第 i 台机器的工作能力为 Ai。保罗阴谋破坏一些机器,使得约翰的

工作效率变低。保罗可以任意选取一段编号连续的机器,使它们停止工作。但这样的破坏只能搞一次,
而且保罗无法破坏第一台或最后一台机器。请问他该破坏哪些机器才能让剩下机器的工作效率的平均
数最小?为了显示存在感,保罗至少必须破坏一台机器。
输入
* Line 1: The integer N.
* Lines 2..1+N: Line i+1 contains M_i.
输出
* Line 1: The lowest possible average Farmer Paul can achieve, rounded to 3 digits after the decimal point, and printed with 3 digits after the decimal point.

样例输入

5
5
1
7
8
2
样例输出
2.667
OUTPUT DETAILS: The optimal solution is to remove the 7 and 8, leaving 5, 1, and 2, whose average is 8/3.

可以发现,答案的大小是满足单调性的,那么真的是二分答案。

设当前check的答案是x,sum[i]为前缀和。
那么满足(sum[n]-sum[r]+sum[l-1])/(n-(r-l+1))<=x即可满足
化简一下sum[n]-x*n-(sum[r]-r*x)+(sum[l-1]-(l-1)*x)<=0;
考虑如何把效率降为O(N),可以发现sum[l-1]-(l-1)*x越小越好。而且r在不断枚举,因此l就可以不断更新成最小值,就不必枚举了。

#include
#include
#include
#include
#include
#define N 100005using namespace std;int n,sum[N];double ans;inline bool check(double x){ double k=(double)sum[1]-x,l=(double)sum[n]-(double)x*n; for(int i=2;i
>n; for(int i=1;i<=n;i++)scanf("%d",&sum[i]),sum[i]+=sum[i-1]; double l=0.0,r=(double)sum[n],mid; while(r-l>1e-8) { mid=(l+r)/2; if(check(mid))ans=mid,r=mid; else l=mid; } printf("%.3lf",ans);}

转载于:https://www.cnblogs.com/QTY2001/p/7632679.html

你可能感兴趣的文章
ruby 模块 的引入
查看>>
CI Weekly #21 | iOS 持续集成快速入门指南
查看>>
xml 校验
查看>>
Jquery获取输入框属性file,ajax传输后端,下载图片
查看>>
深入浅出Visual_C动态链接库(Dll)编程(宋宝华)----整理(word)
查看>>
docker运行环境安装-后续步骤(二)
查看>>
Python学习——02-Python基础——【3集合与函数】
查看>>
NPOI导出excel表格应用
查看>>
tensorflow从入门到放弃-0
查看>>
解锁scott用户
查看>>
多态的理解
查看>>
AspNet Core 发布到Linux系统和发布IIS 注意项
查看>>
Windows添加.NET Framework 3.0 NetFx3 失败 - 状态为:0x800f0950
查看>>
隐藏显示终端的光标(shell echo,linux c printf)
查看>>
SQL Server 存储过程
查看>>
JSP 标准标签库(JSTL)(JSP Standard Tag Library)
查看>>
导入项目遇到的问题: Some projects cannot be imported because they already exist in the workspace....
查看>>
华为:字符集合
查看>>
用Okhttp框架登录之后的Cookie设置到webView中(转)
查看>>
Java_Activiti5_菜鸟也来学Activiti5工作流_之入门简单例子(一)
查看>>