问题 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<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100005
using 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;i++){if(l-sum[i]+i*x+k<=0)return 1;k=min(k,(double)sum[i]-i*x);}return 0;
}
int main()
{cin>>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

二分答案 [Usaco2014 Mar]Sabotage相关推荐

  1. [Usaco2014 Mar]Sabotage

    [Usaco2014 Mar]Sabotage 题目 Farmer John"s arch-nemesis, Farmer Paul, has decided to sabotage Far ...

  2. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

    一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...

  3. BZOJ 1639: [Usaco2007 Mar]Monthly Expense 月度开支( 二分答案 )

    直接二分答案然后判断. ----------------------------------------------------------------------------- #include&l ...

  4. UVA1396 Most Distant Point from the Sea(AM - ICPC - Tokyo - 2007)(计算几何,半平面交 + 二分答案)

    整理的算法模板合集: ACM模板 题目传送门 见<训练指南>P279 很明显就是一个二分答案,它问的是最远的点,直接枚举因为这里都是double类型的数所以有无限个点,我们可以直接二分. ...

  5. UVA1146 / LA3211(ACM-ICPC 2004 Europe - Southwestern) Now or later(2-SAT问题 + 二分答案)

    题目要求为 最大化最小值,很明显就是二分答案. 题目中每个飞机 要么是一种状态(早),要么是另一种状态(晚),考虑 2-SAT. 我们二分答案,二分着陆时间间隔的最小值 x. 枚举每两个飞机 p , ...

  6. 解题报告:luoguP2868 Sightseeing Cows G(最优比率环,负环判定,二分答案)

    根据题意,我们要环上各点权值之和除以各边权值之和最大. 求最大答案,很明显可以使用二分答案.那么我们假设当前答案为 x,如果有更大的答案,那么方程就可以按下图转换: 也就是说如果有更大的答案,则有一个 ...

  7. P2759 奇怪的函数(二分答案,数学运算)

    P2759 奇怪的函数 范围2e92e92e9,直接枚举肯定超时,正着直接求答案求不出来,那么运用逆向思维,直接二分答案判断即可.这道题涉及简单的数学运算. 要xx>=nx^x>=nxx& ...

  8. P3743 kotori的设备(二分答案,思维,线性)难度⭐⭐⭐

    题目链接 题目背景 kotori 有 n 个可同时使用的设备. 题目描述 第 i 个设备每秒消耗ai个单位能量.能量的使用是连续的,也就是说能量不是某时刻突然消耗的,而是匀速消耗.也就是说,对于任意实 ...

  9. 【基础算法】二分法(二分答案,二分查找),三分法,Dinkelbach算法,算法详解+例题剖析

    目录 一 . 二分法 二分搜索得要求: 二分查找步骤: 二分答案: 玄学的二分(二分答案) 二 . 三分法 例题 三.01分数规划问题相关算法与题目讲解(二分法与Dinkelbach算法) 一 . 二 ...

最新文章

  1. CSS3 overflow-x 属性
  2. 原创数据结构算法Flash动画演示课件-Action Script(AS)脚本实现
  3. C++指针数组、数组指针、数组名及二维数组技巧汇总
  4. linux shel的学习网站(推荐)
  5. 【很详细】JDK安装与环境变量配置
  6. 多项式输出-Java
  7. 服务器能进系统滴滴响,曙光服务器开机滴滴响
  8. java学习笔记2022.1.13
  9. android已停止三星,急,android离线打包程序三星下总报 程序 已停止
  10. Docker三剑客之docker-compose
  11. (转)使用JMeter进行Web压力测试
  12. lintcode-76-最长上升子序列
  13. 凸优化第六章逼近与拟合 6.2最小范数问题
  14. IBM 人工智能黑科技
  15. 单片机PWM舵机控制原理
  16. python 统计微信群未接龙
  17. 论文解读 - 城市自动驾驶车辆运动规划与控制技术综述 (第1部分)
  18. c语言中大写字母A转小写,C语言 | 大写字母A转换为小写a
  19. 【代老婆发】你是中介,你姐是黑中介,你们全家都是我爱我家的中介
  20. 考研数学二-汤家凤中值定理

热门文章

  1. 快排java实现图文解释
  2. 2015年超强倒车入库技巧!完整图片解说
  3. 使用USBasp给arduino烧写bootloader
  4. Linux解压命令tar -zxvf中参数zxvf解释
  5. 华为机试题:统计一共有多少套五福
  6. HTML5页面元素和属性大全,自行定位
  7. 使用腾讯qq邮箱SMTP服务遇到的坑
  8. Vue 高阶组件和 mixins
  9. Cooki和session的区别和原理:
  10. python 的 object 与type的关系