Rogu P2115 [usaco 14mar] destroys Sabotage and p2115sabotage

Source: Internet
Author: User

Rogu P2115 [usaco 14mar] destroys Sabotage and p2115sabotage
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 wocould be better for him to avoid sabotage entirely.

Fortunately, Farmer John has learned of Farmer Paul's edevil 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.

Paul, the top enemy of Farmer John, decided to destroy John's milking equipment. The milking equipment is arranged in a row with N (3 <= N <= 100000) milking machines, where the I-th milking machine produces M_ I Unit (1 <= M_ I <= 10,000) milk.

Paul plans to cut off a continuous milking machine from the I-th milking machine to the j-th milking machine (2 <= I <= j <= N-1 ). Note that he does not want to disconnect the first or last milking machine because it will make his plan too easy to find. Paul's goal is to minimize the average milk yield of other machines. Paul planned to remove at least one milking machine.

Calculate the minimum average milk yield for the remaining machines.

Input/Output Format

Input Format:

 

Row 1st: an integer N.

Row 2nd to N + 1: Row I + 1 contains an integer M_ I.

 

Output Format:

 

Row 1st: a real number, which indicates the minimum value of the average milk yield and retains three decimal places (rounded ).

 

Input and Output sample input sample #1: Copy
551782
Output example #1: Copy
2.667
Description

[Example]

Move 7 and 8, leaving 5, 1, 2, and the average value is 8/3.

[Data scale and Conventions]

For 30% of data, N <= 1,000.

For 50% of data, N <= 10,000.

For 100% of data, 3 <= N <= 100,000, 1 <= M_ I <= 10,000.

[Time and Space restrictions]

0.2 s/128 M

 

 

For the first second of the realistic number type, refer to the question

The key to this question is how to maintain the minimum average compared to the binary classification of the real number type.

Let's remove the [I, j] interval, and remove the sum [j]-sum [I-1], sum [n]-(sum [j]-sum [I-1]),

Parentheses, sum [n]-sum [j] + sum [I-1] (that is, the sum of [j, n] plus the sum of [1, I-1 );

The sum divided by the remaining number is the average value, and the remaining number is n-(j-I + 1 ).

So (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) is a constant.

Maintain with one variable for (sum [I-1]-x (I-1)

For (sum [j]-xj) Enumeration

 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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.