https://www.luogu.org/problem/show?pid=2115

题目描述

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。

假设删除区间[i,j]

(sum[n]-sum[j]+sum[i-1])/(n-j+i-1)<=ans

即(sum[n]-sum[j]+sum[i-1])-(n-j+i-1)* ans<=0

有一个i,j满足条件就行

sum[n]-n*ans-sum[j]+j*ans+sum[i-1]-(i-1)*ans<=0

二分ans

枚举j

sum[i-1]-(i-1)*ans这一块肯定是越小越好

枚举j的时候顺便记录这一块的最小值

#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100001
#define eps 1e-5
int n,m[N],sum[N];
double minn;
bool check(double k)
{
minn=sum[]-k;
for(int j=;j<n;j++)
{
if(sum[n]-k*n-sum[j]+k*j+minn<=) return true;
minn=min(minn,sum[j]-k*j);
}
return false;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&m[i]),sum[i]=sum[i-]+m[i];
double l=,r=,mid;
while(r-l>eps)
{
mid=(l+r)/;
if(check(mid)) r=mid-eps;
else l=mid+eps;
}
printf("%.3lf",l);
}

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

  1. 洛谷P2115 [USACO14MAR]破坏Sabotage

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

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

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

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

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

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

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

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

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

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

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

  7. [USACO14MAR]破坏Sabotage

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

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

    传送门 题解:计算欧几里得距离,Krusal加入边权大于等于c的边,统计最后树的边权和. 代码: #include<iostream> #include<cstdio> #in ...

  9. P2115 [USACO14MAR]破坏Sabotage

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

随机推荐

  1. Linux下实现Rsync目录同步备份

    需求:对于开发机器做目录的数据备份 测试机IP:192.168.1.100   WEB目录:/bckup/ 下面我将用一台机器来备份上面测试机 /bckup下的所有数据,并实现时时同步 备份机器IP: ...

  2. MFC常用数据类型

    下面这些是和Win32程序共同使用的数据类型BOOL:布尔值,取值为TRUE or FALSEBSTR:32-bit 字符指针BYTE:8-bit整数,未带正负号COLORREF:32-bit数值,代 ...

  3. Response.End方法

    文章:在try...catch语句中执行Response.End()后如何停止执行catch语句中的内容 调用Response.End()方法能保证,只输出End方法之前的内容. 调用Context. ...

  4. iOS开发Interface Builder技巧

    1.使view的Size与view中的Content相适应:选中任意的一个view,然后Editor->Size to Fit Content,或者简单的按 ⌘=接着就会按照下面的规则对选中vi ...

  5. python爬虫 --- 简书评论

    某些网站的一些数据是通过js加载的 ,所以爬取下来的数据拿不到, 找到评论的地址 .进行请求获取评论数据 #coding=utf-8 import json import requests def r ...

  6. vc6.0批量加注释

    MATLAB批量加注释的方法非常简单明了,加注释是ctrl+R,去注释是ctrl+T 然后在VC中我对一条一条加注释的方法非常烦恼,我想也许会有简单的方法可以批量家注释.果然,先贴代码 '------ ...

  7. c# 调用c++ 使用指针传递的时候

    http://www.cnblogs.com/warensoft/archive/2011/12/09/warenosoft3d.html 上面这篇文章很好解释了. 简单记录一下: 1. 声明  注意 ...

  8. 浅析Docker容器的应用场景

    本文来自网易云社区 作者:娄超 过去几年开源界以openstack为代表的云计算持续火了好久,这两年突然又冒出一个叫Docker的容器技术,其发展之迅猛远超预料.网上介绍Docker容器的文章已经很多 ...

  9. 第27天:js-表单获取焦点和数组声明遍历

    一.表单 1.this指事件的调用者2.input.value 表单更换内容3.innerHTML更换盒子里的内容,文字.标签都能换.4.isNaN("12")如果里面的不是个数字 ...

  10. 第17天:CSS引入、选择器优先级(中级)

    一.CSS 位置 1.行内式  css <div class="fr" style="color:red;">aa</div> 2. 内 ...