zoukankan      html  css  js  c++  java
  • P2115 [USACO14MAR]破坏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(3<= N <=100000)台挤奶机,其中第i个台挤奶机生产M_i单位(1 <= M_i<=10,000)的牛奶。

    保罗计划切断一段连续的挤奶机,从第i台挤奶机到第j台挤奶机(2<= i<= j<= N-1)。注意,他不希望断开第一台或最后一台挤奶机,因为这将会使他的计划太容易被发现。保罗的目标是让其余机器的平均产奶量最小。保罗计划除去至少1台挤奶机。

    请计算剩余机器的最小平均产奶量。

    输入格式

    第 1 行:一个整数 N。

    第 2 到 N+1 行:第 i+1 行包含一个整数 M_i。

    输出格式

    第 1 行: 一个实数, 表示平均牛奶产量的最小值, 保留三位小数 (四舍五入)。

    输入输出样例

    输入 #1
    5
    5
    1
    7
    8
    2
    输出 #1
    2.667

    说明/提示

    【样例说明】

    移去 7 和 8,剩下 5, 1, 2,平均值为 8/3。

    【数据规模和约定】

    对于 30%的数据,N <= 1,000。

    对于 50%的数据,N <= 10,000。

    对于 100%的数据,3 <= N <= 100,000,1 <= M_i <= 10,000。

    【时空限制】

    1s/256M

    让我们来分析一下题目,显然要使用到前缀和(这种区间和应该都要用......)

    我们设sum[i],表示前i个数的和,i,j是Paul破坏的区间

    那么,我们的答案就是:(sum[n]-(sum[j]-sum[i-1]))/(number-(j-i+1))

    那么有显然,这道题是二分答案的题目,所以说我们的  l=0,r=sum[n],mid=(l+r)>>1

    好,接下来就是关键了,敲黑板

    我们已经知道二分的复杂度是log(n),那么如果枚举i,j,那么时间复杂度就会升级为O(n*n*log(n))

    再看看数据范围,毫无疑问的超时了

    那么怎么办呢,既然mid是当前的最大平均值,我们创建一个数组C

    C[i]=sum[i]-mid*i  具体内容见下

    我们可以知道

    (sum[n]-(sum[j]-sum[i-1])/(n-(j-i+1))<=mid

    移项((n-j+i-1)恒>0):

    sum[n]-sum[j]+sum[i-1]<=mid*(n-j+i-1)

    sum[n]-sum[j]+sum[i-1]<=mid*n-mid*j+mid*i-mid

    sum[n]-mid*n-(sum[j]-mid*j)+sum[i-1]-mid*(i-1)<=0

    可以发现,之前的C是一个优化,整个式子就是

    C[n]-C[j]+C[i-1]<=0

    又可以转换为:C[n]<=C[j]-C[i-1]

    所以,C[j]要尽可能大,C[i-1]要尽可能小

    那么也就是说,C[j]=max(C),C[i-1]=min(C)

    #include<bits/stdc++.h>
    using namespace std;
    
    const int N=300002;
    int number,sum[N];
    double C[N],l,r,minn[N],maxx[N];
    
    int read(){
        int s=0,w=1;char ch=getchar();
        while(ch<'0'||ch>'9')w=(ch=='-')?-1:1,ch=getchar();
        while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
        return s*w;
    }
    
    bool check(double now){
        for(int i=1;i<=number;i++)C[i]=(double)(sum[i]-now*i);
        minn[0]=1e12;maxx[number]=-1e12;
        for(int i=1;i<=number-2;i++)minn[i]=min(minn[i-1],C[i]);
        for(int i=number-1;i>1;i--)maxx[i]=max(maxx[i+1],C[i]);
        for(int i=2;i<number;i++)
           if(maxx[i]-minn[i-1]>C[number])return true;
        return false;
    }
    
    int main(){
        number=read();
        for(int i=1;i<=number;i++)sum[i]=sum[i-1]+read();
        l=0;r=(double)(sum[number]);
        while(l+0.000001<=r){
            double mid=(l+r)/2.0;
            if(!check(mid))l=mid;
            else r=mid;
        }
        printf("%.3lf",l);
        return 0;
    }

     

  • 相关阅读:
    C++使用GDI+实现图片格式转换
    易语言调用C++写的DLL
    【PE】手动给PE文件添加一段代码MessageBoxA
    SHGetSpecialFolderLocation获取开始文件夹
    MySQL导入导出数据
    MySQL5.7修改密码
    Elasticsearch 基于external的乐观锁的版本控制
    Elasticsearch修改字段类型 (_reindex)
    es 迁移数据, 重建索引
    SHELL
  • 原文地址:https://www.cnblogs.com/GMSD/p/11577265.html
Copyright © 2011-2022 走看看