题目描述

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(3<= N <=100000)台挤奶机,其中第i个台挤奶机生产M_i单位(1 <= M_i<=10,000)的牛奶。

保罗计划切断一段连续的挤奶机,从第i台挤奶机到第j台挤奶机(2<= i<= j<= N-1)。注意,他不希望断开第一台或最后一台挤奶机,因为这将会使他的计划太容易被发现。保罗的目标是让其余机器的平均产奶量最小。保罗计划除去至少1台挤奶机。

请计算剩余机器的最小平均产奶量。

输入输出格式

输入格式:

第 1 行:一个整数 N。

第 2 到 N+1 行:第 i+1 行包含一个整数 M_i。

输出格式:

第 1 行: 一个实数, 表示平均牛奶产量的最小值, 保留三位小数 (四舍五入)。

输入输出样例

输入样例#1: 复制

5
5
1
7
8
2
输出样例#1: 复制

2.667

说明

【样例说明】

移去 7 和 8,剩下 5, 1, 2,平均值为 8/3。

【数据规模和约定】

对于 30%的数据,N <= 1,000。

对于 50%的数据,N <= 10,000。

对于 100%的数据,3 <= N <= 100,000,1 <= M_i <= 10,000。

【时空限制】

0.2s/128M

第一次写实数类型的二分,参考了一下题解

相比于实数类型的二分,这道题的关键在于如何维护最小平均数

设去掉[i,j]区间,去掉的和就是sum[j]-sum[i-1],剩下的和就是sum[n]-(sum[j]-sum[i-1]),

去括号,sum[n]-sum[j]+sum[i-1](也就是[j,n]的和加上[1,i-1]的和);

剩下的和除以剩下的个数就是平均值,剩下的个数n-(j-i+1)。

那么 (sum[n]-sum[j]+sum[i-1])/(n-j+i-1)<=x。

sum[n]-sum[j]+sum[i-1]<=xn-xj-x(i-1);

(sum[n]-xn)-(sum[j]-xj)+(sum[i-1]-x(i-1))<=0; */

对于(sum[n]-xn)是个常数

对于(sum[i-1]-x(i-1))用一个变量维护

对于(sum[j]-xj)枚举

 #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=;
inline int read()
{
char c=getchar();int x=,f=;
while(c<''||c>'') {if(c=='-')f=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-,c=getchar();return x*f;
}
int n;
int sum[MAXN];
bool check(double val)
{
double now=123456789.00;
double nowmin=sum[]-val;
double sumn=sum[n]-val*n;
for(int i=;i<=n-;i++)
{
if(sumn-sum[i]+val*i+nowmin<=) return ;
if(sum[i-]-val*(i-)<nowmin) nowmin=sum[i-]-val*(i-);
}
return ;
}
int main()
{
n=read();
for(int i=;i<=n;i++) sum[i]=read(),sum[i]=sum[i]+sum[i-];
double l=,r=,ans=;
while(r-l>1e-)
{
double mid=(r+l)/2.0;
if(check(mid)) r=mid;
else l=mid;
}
printf("%.3lf",r);
return ;
}

洛谷P2115 [USACO14MAR]破坏Sabotage的更多相关文章

  1. 洛谷2115 [USACO14MAR]破坏Sabotage

    https://www.luogu.org/problem/show?pid=2115 题目描述 Farmer John's arch-nemesis, Farmer Paul, has decide ...

  2. P2115 [USACO14MAR]破坏Sabotage

    题意:给你一个正整数序列,让你删去一段区间内的数[l,r] $1<l\le r <n$ 使得剩余的数平均值最小$n\le 10^5$ 1.不难想到暴力,用前缀和优化$O(n^2)$ #in ...

  3. 洛谷【P2115】[USACO14MAR]破坏Sabotage

    我对二分的理解:https://www.cnblogs.com/AKMer/p/9737477.html 题目传送门:https://www.luogu.org/problemnew/show/P21 ...

  4. 洛谷P2115 Sabotage G 题解

    题目 [USACO14MAR]Sabotage G 题解 本蒟蒻又来了,这道题可以用二分答案来解决.我们可以设答案最小平均产奶量为 \(x \ (x \in[1,10000])\) .然后二分搜索 \ ...

  5. [USACO14MAR]破坏Sabotage 二分答案

    题目描述 Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipmen ...

  6. LuoguP2115 [USACO14MAR]破坏Sabotage【二分答案】By cellur925

    本来是想找一道生成树的题做的...结果被洛咕的标签骗到了这题...结果是二分答案与生成树一点mao关系都没有.... 题目大意:给你一个序列,请你删去某一个$l~r$区间的值($2<=i< ...

  7. 洛谷——P2212 [USACO14MAR]浇地Watering the Fields

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  8. 洛谷 P2212 [USACO14MAR]浇地Watering the Fields 题解

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  9. [USACO14MAR]破坏Sabotage

    还是二分答案,发现我的$check$函数不太一样,来水一发题解 列一下式子 $$\frac{sum-sum[l,r]}{n-(r-l+1)}<=ans$$ 乘过去 $$sum-sum[l,r]& ...

随机推荐

  1. AD域导入导出命令

    AD域 批量组织机构.用户导入导出 参考网站 https://technet.microsoft.com/zh-cn/library/cc753447(v=ws.11).aspx 导入所有命令 均cm ...

  2. CDH5.14操作指南

    1.Cdh 5.14介绍 2.版本发布概要 3.安装要求 4.Cloudera Manager介绍 5.Cloudera 安装 6.Cloudera 管理 7.Cloudera Navigator C ...

  3. RMAN备份脚本--DataGuard primary

    单机环境全备   export ORACLE_BASE=/oracle export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1 export ORACL ...

  4. Debian9.5 WPS for Linux字体配置(字体缺失解决办法)

    启动WPS for Linux后,出现提示"系统缺失字体" . 出现提示的原因是因为WPS for Linux没有自带windows的字体,只要在Linux系统中加载字体即可. 具 ...

  5. Mac配置PHP环境

    本文章来自:http://blog.csdn.net/wj_november/article/details/51417491 本人使用的是:MacOs 10.12.3,根据如上操作已经安装成功,感谢 ...

  6. 包及常用模块(time、datetime、random、sys)

    什么是包?‘ #官网解释 Packages are a way of structuring Python’s module namespace by using “dotted module nam ...

  7. Unity 给FindGameObjectsWithTag排序

    GameObject[] patrol = GameObject.FindGameObjectsWithTag ("Player").OrderBy (g => g.tran ...

  8. Spring Boot学习总结(1)——Spring Boot入门

    摘要:Spring Boots是为了帮助开发人员很容易的创建出独立运行和产品级别的基于 Spring 框架的应用. 从 Spring Boot 项目名称中的 Boot 可以看出来,Spring Boo ...

  9. [Teamcenter 2007 开发实战] 调用web service

    前言 在TC的服务端开发中, 能够使用gsoap 来调用web service. 怎样使用 gsoap  , 參考 gsoap 实现 C/C++ 调用web service 接下来介绍怎样在TC中进行 ...

  10. Android插件实例——360 DroidPlugin具体解释

    在中国找到钱不难,但你的一个点子不意味着是一个创业.你谈一个再好的想法,比方我今天谈一个创意说,新浪为什么不收购GOOGLE呢?这个创意非常好.新浪一收购GOOGLE.是不是新浪就变成老大了?你从哪儿 ...