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

本文解析了Codeforces上一道关于计算连续天数平均温度最大值的问题,通过前缀和及双层循环的方法,实现了对任意长度连续子序列的平均值计算,以找到满足条件的最大平均温度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

原题链接: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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值