[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. luoguP2939 [USACO09FEB]改造路Revamping Trails

    约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 速公路. ...

  2. JAVA Swing 事件监听

    EventListner 接口 它是一个标记接口,每一个监听器接口扩展.这个类定义在java.util包. 类声明 以下是声明java.util.EventListener接口: public int ...

  3. codevs3728联合权值(LCA)

    3728 联合权值  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 输入描述 Input Des ...

  4. [Swift通天遁地]九、拔剑吧-(4)使用开源类库创建可滑动的Segment分段控件

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. HTTP Status 500 - Could not write content: could not initialize proxy - no Session

    分析出现no Session错误的原因以及给出解决方案: 使用SpringMVC + JSON数据返回时,经常会出现no Session的错误: 报错原因:因为懒加载在提取关联对象的属性值的时候发现E ...

  6. 解决:xxx is not in the sudoers file.This incident will be reported.的解决方法

    Linux中普通用户用sudo执行命令时报”xxx is not in the sudoers file.This incident will be reported”错误,解决方法就是在/etc/s ...

  7. UNIX环境高级编程--5

    标准I/O库流和FILE对象:    所有I/O函数都是围绕文件描述符的.当打开一个文件时,即返回一个文件描述符,然后该文件描述符就用于后续的I/O操作.当用标准I/O库打开或者创建一个文件时,我们已 ...

  8. easyui textbox 内容改变事件 增加oninpu 类似事件,

    //======================利用easyui验证功能,进行内容变化监控=== =============$(function () { var CustomerService = ...

  9. WinForms_ListView中获取选中项数据值

    string value = listList.SelectedItems[0].SubItems[1].Text;//获取首行listview的值 string va = listList.Sele ...

  10. JS——select标签

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...