[Usaco2014 Mar]Sabotage
[Usaco2014 Mar]Sabotage
题目
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。保罗阴谋破坏一些机器,使得约翰的
工作效率变低。保罗可以任意选取一段编号连续的机器,使它们停止工作。但这样的破坏只能搞一次,
而且保罗无法破坏第一台或最后一台机器。请问他该破坏哪些机器才能让剩下机器的工作效率的平均
数最小?为了显示存在感,保罗至少必须破坏一台机器。INPUT
Line 1: The integer N.
Lines 2..1+N: Line i+1 contains M_i.
OUTOUT
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.
SAMPLE
INPUT
5
5
1
7
8
2
OUTPUT
2.667
解题报告
实数二分的力量= =
我们二分答案,得到最小的平均值,然后验证该平均值是否合法
重点就落到如何验证了
设当前验证的平均值为$avr$
那么对于$a_{i}$来说,存在$b_{i}=a_{i}-avr$,那么,当$b_{i}>0$时,$a_{i}>avr$,$b_{i}<0$时,$a_{i}<avr$
那么,对于每一个$avr$,我们可以处理出来这个$b$数组
我们考虑,既然我们只能去掉一段连续的区间,那么剩下的也一定是两个连续区间
所以,我们可以处理出来每个点的前缀和与后缀和的最小值,(这里的最小值是指,在前(后)缀不断更新时,更新该点之前的历史最小值),然后我们判断,是否存在一点,使其前缀和最小值与后缀和最小值的和$sum$满足$sum<=0$,这样,显然就一定存在一个包含该点的区间,其左端点的前缀和与后缀和之和$tot$满足$tot<=0$,这两个和即分别为两端区间的区间和
既然存在了这样的处于两端的两个区间,那么就存在这两个区间的平均值小于该平均值
即:该平均值合法
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
inline int read(){
int sum();
char ch(getchar());
for(;ch<''||ch>'';ch=getchar());
for(;ch>=''&&ch<='';sum=sum*+(ch^),ch=getchar());
return sum;
}
int n;
double a[],sum;
double b[];
const double eps(1e-);
double pre[],nxt[];
inline bool check(double x){//cout<<x<<endl;
for(int i=;i<=n;++i)
b[i]=a[i]-x,pre[i]=nxt[i]=1e16;/*,cout<<i<<' '<<b[i]<<' ';cout<<endl;*/
double sum(),mn(1e16);
for(int i=;i<=n;++i)
pre[i]=min(sum,mn),sum+=b[i],mn=min(mn,sum);
sum=,mn=1e16;
for(int i=n;i>;--i)
nxt[i]=min(sum,mn),sum+=b[i],mn=min(mn,sum);
for(int i=;i<n;++i)
if(pre[i]+nxt[i]<=eps)
return true;
return false;
}
int main(){
n=read();
for(int i=;i<=n;++i)
a[i]=read(),sum+=a[i];
double l(),r(sum),ans;
while(r-l>=eps){
double mid((l+r)/2.0);//cout<<l<<' '<<r<<' '<<mid<<endl;
if(check(mid))
r=mid,ans=mid;
else
l=mid;
}
printf("%.3lf",ans);
}
[Usaco2014 Mar]Sabotage的更多相关文章
- BZOJ 3477: [Usaco2014 Mar]Sabotage( 二分答案 )
先二分答案m, 然后对于原序列 A[i] = A[i] - m, 然后O(n)找最大连续子序列和, 那么此时序列由 L + mx + R组成. L + mx + R = sum - n * m, s ...
- 【二分 贪心】bzoj3477: [Usaco2014 Mar]Sabotage
科学二分姿势 Description Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's mi ...
- bzoj3477[Usaco2014 Mar]Sabotage
题意 给出一个长为n的正整数序列(n<=1e5),要求选出一个非空前缀和一个非空后缀(这两段不能够加起来组成整个序列),使得这个前缀和后缀中的所有数字一起求平均数的结果最小 分析 最大/最小化平 ...
- 【bzoj】3477: [Usaco2014 Mar]Sabotage 01分数规划
这题算是01分数规划吧2333 sum-a[i]*x[i]=c*(n-x[i]) 化简一下就是sum-(a[i]-c)*x[i]-nc=0,每次找最大的(a[i]-c)*x[i](子段和),如果结果& ...
- BZOJ3479: [Usaco2014 Mar]Watering the Fields
3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 81 Solved: ...
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )
MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...
- BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案
BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案 题意: 约翰的牧场里有 N 台机器,第 i 台机器的工作能力为 Ai.保罗阴谋破坏一些机器,使得约翰的工作效率变低.保罗可 ...
- bzoj 3479: [Usaco2014 Mar]Watering the Fields
3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 174 Solved ...
- BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离
BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离 Description It's a hot summer day, and Bessie the c ...
随机推荐
- [HNOI2011]XOR与路径
https://zybuluo.com/mdeditor#1094266 标签(空格分隔): 高斯消元 期望 题面 从 1 号节点开始,以相等的概率,随机选择与当前节点相关联的某条边,并沿这条边走到下 ...
- 61.员工信息管理Extjs 页面
1.员工信息管理jsp <%@ page language="java" pageEncoding="UTF-8"%> <script typ ...
- 02_jni_hello_c函数介绍
介绍NDK平台都有哪些工具.通过NDK这套工具做安卓下的JNI开发. 可能有一些需求更适合通过C去做,有一些功能要通过C去实现.一个安卓程序,它本身还是一个Java应用.有一些功能/方法不通过Java ...
- P3291 [SCOI2016]妖怪
传送门 我数学的确白学了--这种题目竟然一点思路都没有-- 首先可以把每个妖怪看成二维平面上的一个点,那么每一个环境\((a,b)\)就可以看成一条斜率\(k=-\frac{b}{a}\)的过该点的直 ...
- 零基础如何学习Java和web前端
今天说一下零基础到底能不能学习Java,为什么有的人说学不了呢,那么接下来我为大家揭晓,零基础到底适合不适合学习Java. 零基础学习Java的途径第一个就是看视频,然后就是看书,或者在线下报个培训班 ...
- python自动化测试学习笔记-1
一.什么是自动化 自动化测试是把以人为驱动的测试行为转化为机器执行的一种过程.直白的就是为了节省人力.时间或硬件资源,提高测试效率,便引入了通过软件或程序自动化执行测试用例进行测试: 二.python ...
- eclipse-html插件的安装
需求:需要在eclipse里面编辑html和jsp,语法高亮和语法提示,自动补全等. 1.下载GEF(依赖包): http://www.eclipse.org/downloads/download.p ...
- 51nod 1340 差分约束
思路: 带未知量的Floyd 很强 http://yousiki.net/index.php/archives/87/ //By SiriusRen #include <bits/stdc++. ...
- Python 如何在csv中定位非数字和字母的符号
在数据清洗过程中,有时不仅希望去掉脏数据,更希望定位脏数据的位置,例如从csv里面定位非数字和字母单元格的位置,在使用isdigit().isalpha().isalnum()时无法判断浮点数,会将浮 ...
- EF 新增数据时提示it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element
it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionM ...