[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$,这两个和即分别为两端区间的区间和
既然存在了这样的处于两端的两个区间,那么就存在这两个区间的平均值小于该平均值
即:该平均值合法
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 inline int read(){ 6 int sum(0); 7 char ch(getchar()); 8 for(;ch<'0'||ch>'9';ch=getchar()); 9 for(;ch>='0'&&ch<='9';sum=sum*10+(ch^48),ch=getchar()); 10 return sum; 11 } 12 int n; 13 double a[100005],sum; 14 double b[100005]; 15 const double eps(1e-8); 16 double pre[100005],nxt[100005]; 17 inline bool check(double x){//cout<<x<<endl; 18 for(int i=1;i<=n;++i) 19 b[i]=a[i]-x,pre[i]=nxt[i]=1e16;/*,cout<<i<<' '<<b[i]<<' ';cout<<endl;*/ 20 double sum(0),mn(1e16); 21 for(int i=1;i<=n;++i) 22 pre[i]=min(sum,mn),sum+=b[i],mn=min(mn,sum); 23 sum=0,mn=1e16; 24 for(int i=n;i>0;--i) 25 nxt[i]=min(sum,mn),sum+=b[i],mn=min(mn,sum); 26 for(int i=2;i<n;++i) 27 if(pre[i]+nxt[i]<=eps) 28 return true; 29 return false; 30 } 31 int main(){ 32 n=read(); 33 for(int i=1;i<=n;++i) 34 a[i]=read(),sum+=a[i]; 35 double l(0),r(sum),ans; 36 while(r-l>=eps){ 37 double mid((l+r)/2.0);//cout<<l<<' '<<r<<' '<<mid<<endl; 38 if(check(mid)) 39 r=mid,ans=mid; 40 else 41 l=mid; 42 } 43 printf("%.3lf",ans); 44 }