[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的更多相关文章

  1. BZOJ 3477: [Usaco2014 Mar]Sabotage( 二分答案 )

    先二分答案m, 然后对于原序列 A[i] = A[i] - m,  然后O(n)找最大连续子序列和, 那么此时序列由 L + mx + R组成. L + mx + R = sum - n * m, s ...

  2. 【二分 贪心】bzoj3477: [Usaco2014 Mar]Sabotage

    科学二分姿势 Description Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's mi ...

  3. bzoj3477[Usaco2014 Mar]Sabotage

    题意 给出一个长为n的正整数序列(n<=1e5),要求选出一个非空前缀和一个非空后缀(这两段不能够加起来组成整个序列),使得这个前缀和后缀中的所有数字一起求平均数的结果最小 分析 最大/最小化平 ...

  4. 【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](子段和),如果结果& ...

  5. BZOJ3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 81  Solved: ...

  6. BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )

    MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...

  7. BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案

    BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案 题意: 约翰的牧场里有 N 台机器,第 i 台机器的工作能力为 Ai.保罗阴谋破坏一些机器,使得约翰的工作效率变低.保罗可 ...

  8. bzoj 3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved ...

  9. 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 ...

随机推荐

  1. 【关键字】volatile

    volatile 修饰的关键字,确保编译器不对成员变量进行任何优化: private volatile double d; // No optimization

  2. SQL service

    依赖关系解决 ============================================================================================= ...

  3. window.onload的使用

    window.onload:当页面加载的时候可以调用某些函数 例如: 1.最简单的调用方式 直接写到html的body标签里面,如: <html> <body onload=&quo ...

  4. P3187 [HNOI2007]最小矩形覆盖

    传送门 首先这个矩形的一条边肯定在凸包上.那么可以求出凸包然后枚举边,用类似旋转卡壳的方法求出另外三条边的位置,也就是求出以它为底最上面最右边最左边的点的位置.离它最远的点可以用叉积求,最左最右的可以 ...

  5. [App Store Connect帮助]三、管理 App 和版本(2.3)输入 App 信息:提供自定许可协议

    Apple 提供了适用于所有地区的标准 EULA(最终用户许可协议).如果您不提供自定许可协议,则您的 App 会应用标准 Apple EULA,且该许可协议链接不会显示在您的 App Store 产 ...

  6. 1.1输出“hello,world”

    #include<iostream> using namespace std; int main() { cout<<"Hello, World!"< ...

  7. BZOJ 4481

    思路: 等比数列求和 (无穷项) +线段树找逆序对 //By SiriusRen #include <bits/stdc++.h> ; ; ],ans; struct Node{int x ...

  8. 使用Quartz2.2.3做持久化,启动程序后,控制台报错问题

    该错误是由mysql-connector-java.jar版本太低导致. MLog clients using log4j logging. Initializing c3p0-0.9.1.1 [bu ...

  9. semantic、vue 使用分页组件和日历插件

    最近正在试试semantic-ui,结合了vue,这里忍不住吐槽semantic和vue的友好度简直不忍直视,不过既然用了,这里就分享几个用到的插件了 1.分页组件(基于vue) var pageCo ...

  10. Android文件操作报open failed: EBUSY (Device or resource busy)

    Android删除文件后重新创建时偶尔出现 open failed: EBUSY (Device or resource busy)错误,该错误是Android系统的一个bug,大概的意思类似于win ...