[bzoj3477][Usaco2014 Mar][二分]Sabotage

109 篇文章 4 订阅
55 篇文章 0 订阅

Description

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.

Output

  • 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

Sample Output

2.667

OUTPUT DETAILS: The optimal solution is to remove the 7 and 8, leaving
5, 1, and 2, whose average is 8/3.

题解

感谢波老师
感谢艹老师
感谢cctv,感谢cntv,感谢csdn
一开始看见完全没思路。。如果是求最大子段和的话,万一不够优呢?因为个数的问题不够优
所以,二分平均值吧。对于没被破坏的每台机器,减去平均值后的和一定为0
那么check什么?我们求机器减去平均值后的序列的最大子段和
这样就可以让影响最大化

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=0.00001;
double a[110000],opt;int n;
double b[110000],ans;
bool check(double p)
{
    double sum=0,ret=-999999999,s=0;
    for(int i=1;i<=n;i++){b[i]=a[i]-p;s+=b[i];}
    for(int i=2;i<n;i++)
    {
        if(sum<0)sum=0;
        sum+=b[i];ret=max(ret,sum);
    }
    if(s-ret<=eps)return true;
    return false;
}
int main()
{
    scanf("%d",&n);opt=0;
    for(int i=1;i<=n;i++){scanf("%lf",&a[i]);opt+=a[i];}
    double l=0,r=opt;
    while(l+eps<=r)
    {
        double mid=(l+r)/2;
        if(check(mid)){ans=mid;r=mid;}
        else l=mid;
    }
    printf("%.3lf\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值