洛谷P2115 [USACO14MAR]破坏Sabotage

时间:2022-05-08
本文章向大家介绍洛谷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

第一次写实数类型的二分,参考了一下题解

相比于实数类型的二分,这道题的关键在于如何维护最小平均数

设去掉[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; */

对于(sum[n]-xn)是个常数

对于(sum[i-1]-x(i-1))用一个变量维护

对于(sum[j]-xj)枚举

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 const int MAXN=100001;
 6 inline int read()
 7 {
 8     char c=getchar();int x=0,f=1;
 9     while(c<'0'||c>'9')    {if(c=='-')f=-1;c=getchar();}
10     while(c>='0'&&c<='9')    x=x*10+c-48,c=getchar();return x*f;
11 }
12 int n;
13 int sum[MAXN];
14 bool check(double val)
15 {
16     double now=123456789.00;
17     double nowmin=sum[1]-val;
18     double sumn=sum[n]-val*n;
19     for(int i=2;i<=n-1;i++)
20     {
21         if(sumn-sum[i]+val*i+nowmin<=0)    return 1;
22         if(sum[i-1]-val*(i-1)<nowmin)    nowmin=sum[i-1]-val*(i-1);
23     }
24     return 0;
25 }
26 int main()
27 {
28     n=read();
29     for(int i=1;i<=n;i++)    sum[i]=read(),sum[i]=sum[i]+sum[i-1];
30     double l=1,r=5000000,ans=1;
31     while(r-l>1e-5)
32     {
33         double mid=(r+l)/2.0;
34         if(check(mid))    r=mid;
35         else l=mid;    
36     }
37     printf("%.3lf",r);
38     return 0;
39 }