zoukankan      html  css  js  c++  java
  • P2115 [USACO14MAR]破坏Sabotage

    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。

    【时空限制】

    0.2s/128M

    一看就是二分答案吗,怎么二分呢。

    可以二分枚举平均值x,先维护一个前缀和,按题意要去掉一个区间,是平均值最小,设去掉[i,j]区间,去掉的和就是sum[j]-sum[i-1],剩下的和就是sum[n]-(sum[j]-sum[i-1]),去括号,sum[n]-sum[j]+sum[i-1](也就是[j,n]的和加上[1,i-1]的和);剩下的和除以剩下的个数就是平均值,剩下的个数n-(j-i+1)。

    那么 (sum[n]-sum[j]+sum[i-1])/(n-j+i-1)<=x。                                                      

    sum[n]-sum[j]+sum[i-1]<=xn-xj-x(i-1);

    (sum[n]-xn)-(sum[j]-xj)+(sum[i-1]-x(i-1))<=0; 

     1 #include<cstdio>
     2 #include<algorithm>
     3 
     4 using namespace std;
     5 const int MAXN = 100100;
     6 int sum[MAXN];    //前缀和 
     7 int n;
     8 
     9 bool check(double x)
    10 {
    11     double minv = sum[1]-x*1;    //先假设从1开始 
    12     for (int i=2; i<n; ++i)
    13     {
    14         if (sum[n]-x*n-(sum[i]-x*i)+minv<=0) return true ;
    15         minv = min(minv,sum[i]-x*i);    //因为是加,取小 
    16     }
    17     return false ;
    18 }
    19 int main()
    20 {
    21     scanf("%d",&n);
    22     for (int a,i=1; i<=n; ++i)
    23     {
    24         scanf("%d",&a);
    25         sum[i] = sum[i-1]+a;
    26     }
    27     double l = 0, r = 10000;
    28     while (r-l>1e-5)    //精度到0.00001,只输出三位,所以1e-5就够了 
    29     {
    30         double mid = (l+r)/2.0;
    31         if (check(mid)) r = mid;    //mid可以,就在取小 
    32         else l = mid;
    33     }
    34     printf("%.3lf",r);
    35     return 0;
    36 }
  • 相关阅读:
    马氏距离的深入理解
    Mahalanobis Distance(马氏距离)
    Weka EM 协方差
    数据挖掘、概率分析与决策支持
    二、 Android中gravity与layout_gravity的区别
    一、 Android完全退出应用程序
    python configparse
    时间戳与时间互转
    python argparse
    时间插件
  • 原文地址:https://www.cnblogs.com/mjtcn/p/7094888.html
Copyright © 2011-2022 走看看