zoukankan      html  css  js  c++  java
  • Intense Heat(前缀和或尺取)

    The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are.

    Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows:
    Suppose we want to analyze the segment of n  consecutive days. We have measured the temperatures during these n days; the temperature during i-th day equals ai .
    We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day x to day y, we calculate it as y∑i=xaiy−x+1 (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than k consecutive days. For example, if analyzing the measures [3,4,1,2] and k=3, we are interested in segments [3,4,1], [4,1,2] and [3,4,1,2]   (we want to find the maximum value of average temperature over these segments).
    You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task?

    Input

    The first line contains two integers n and k (1≤k≤n≤5000) — the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively. The second line contains n integers a1, a2, ..., an (1≤ai≤5000) — the temperature measures during given n days.

    Output

    Print one real number — the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than k consecutive days.

    Your answer will be considered correct if the following condition holds: |res−res0|<10−6, where res is your answer, and res0 is the answer given by the jury's solution.
    Example
    Input

    4 3
    3 4 1 2

    Output

    2.666666666666667

    题目意思:求连续不小于k天各区段的平均温度的最大值。

    解题思路:我当时做题的时候是用的前缀和求出连续区段的平均值,之后师哥又提供了一种另一种思路使用尺取的思想,这里给出两种方法的代码。

     前缀和代码:

    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    int a[5005];
    int s[5005];
    int main()
    {
        int n,k,i,t,j,p;
        double ans;
        scanf("%d%d",&n,&k);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        s[0]=a[0];
        for(i=1;i<n;i++)
        {
            s[i]=s[i-1]+a[i];///求前缀和
        }
        p=1;
        ans=0.0;
        for(i=k;i<=n;i++)///i代表天数,至少是k天
        {
            for(j=0;j<=n-i;j++)
            {
                if(j==0)
                {
                    t=s[j+i-1];
                }
                else
                {
                    t=s[j+i-1]-s[j-1];
                }
                if((double)t/i>ans)
                {
                   ans=(double)t/i;
                }
            }
        }
        printf("%lf
    ",ans);
        return 0;
    }

    尺取代码:

     1 #include<stdio.h>
     2 #include<algorithm>
     3 #define INF 0x3f3f3f3f
     4 using namespace std;
     5 int a[5005];
     6 int main()
     7 {
     8     int n,k,i,j,p,q;
     9     double ans,sum;
    10     scanf("%d%d",&n,&k);
    11     for(i=0; i<n; i++)
    12     {
    13         scanf("%d",&a[i]);
    14     }
    15     ans=-INF;
    16     for(i=k; i<=n; i++)///至少是k天最多n天
    17     {
    18         sum=0;
    19         for(j=0; j<i; j++)
    20         {
    21             sum+=a[j];
    22         }
    23         ans=max(ans,sum/i);
    24         q=0;
    25         p=i;
    26         while(p!=n)
    27         {
    28             sum=sum-a[q++];///扩大右端点
    29             sum=sum+a[p++];///扩大左端点,但仍保持长度不变
    30             ans=max(ans,sum/i);
    31         }
    32     }
    33     printf("%lf
    ",ans);
    34     return 0;
    35 }
  • 相关阅读:
    CentOS7.5 防火墙指令
    监控Tomcat
    MySQL.ERROR 1133 (42000): Can't find any matching row in the user table
    Hadoop.之.入门部署
    Jenkins. 安装过程中出现一个错误: No such plugin: cloudbees-folder
    Spring.之.报错:Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
    Spring.之.jar包官网下载
    Linux 安装Eclipse
    Linux SSH 免秘钥登录
    Linux rz命令无效
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9378802.html
Copyright © 2011-2022 走看看