暑假练习赛1——Problem E(前缀和)

 

原题链接:http://codeforces.com/problemset/problem/1003/C

Problem E

Time Limit : 8000/4000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other)

Total Submission(s) : 2   Accepted Submission(s) : 0

Problem Description

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

Input

<p>The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 5000$$$) the number of days in the given period, and the minimum number of days in a segment we consider when calculating <span class="tex-font-style-it">heat intensity value</span>, respectively.</p><p>The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 5000$$$) the temperature measures during given $$$n$$$ days.</p>

Output

<p>Print one real number the <span class="tex-font-style-it">heat intensity value</span>, i. e., the maximum of <span class="tex-font-style-it">average temperatures</span> over all segments of not less than $$$k$$$ consecutive days.</p><p>Your answer will be considered correct if the following condition holds: $$$|res - res_0| < 10^{-6}$$$, where $$$res$$$ is your answer, and $$$res_0$$$ is the answer given by the jury's solution.</p>

Sample Input

 

4 3

3 4 1 2

Sample Output

 

2.666666666666667

===================

题目大意:

给出n个数,从中取m个数得平均值,m最小为k,求最大平均值。

思路:

前缀和+两层循环(第一层枚举m),开始的错因也在于没理解好 k是选数个数的最小值,仅仅认为m=k,so wrong。

最后注意控制一下精度就好了:

洛谷AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int a[100100];
int sum[100100];
int main()
{
    int n,k;
    while(cin>>n>>k)
    {
        memset(a,0,sizeof(a));
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;++i)
        {
            cin>>a[i];
            sum[i]=sum[i-1]+a[i];
        }
        double ans=-9999999.0;
        for(int j=k;j<=n;++j)
            for(int i=j;i<=n;++i)
            {
                double t=(sum[i]-sum[i-j])*1.0/j;
                if(t-ans>1e-8)ans=t;
            }
            printf("%.15lf\n",ans);

    }
    return 0;
}

The end;

猜你喜欢

转载自blog.csdn.net/qq_41661919/article/details/81318595