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[];
int s[];
int main()
{
int n,k,i,t,j,p;
double ans;
scanf("%d%d",&n,&k);
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
}
s[]=a[];
for(i=;i<n;i++)
{
s[i]=s[i-]+a[i];///求前缀和
}
p=;
ans=0.0;
for(i=k;i<=n;i++)///i代表天数,至少是k天
{
for(j=;j<=n-i;j++)
{
if(j==)
{
t=s[j+i-];
}
else
{
t=s[j+i-]-s[j-];
}
if((double)t/i>ans)
{
ans=(double)t/i;
}
}
}
printf("%lf\n",ans);
return ;
}

尺取代码:

 #include<stdio.h>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int a[];
int main()
{
int n,k,i,j,p,q;
double ans,sum;
scanf("%d%d",&n,&k);
for(i=; i<n; i++)
{
scanf("%d",&a[i]);
}
ans=-INF;
for(i=k; i<=n; i++)///至少是k天最多n天
{
sum=;
for(j=; j<i; j++)
{
sum+=a[j];
}
ans=max(ans,sum/i);
q=;
p=i;
while(p!=n)
{
sum=sum-a[q++];///扩大右端点
sum=sum+a[p++];///扩大左端点,但仍保持长度不变
ans=max(ans,sum/i);
}
}
printf("%lf\n",ans);
return ;
}

Intense Heat(前缀和或尺取)的更多相关文章

  1. Lecture Sleep(尺取+前缀和)

    Description 你的朋友Mishka和你参加一个微积分讲座.讲座持续n分钟.讲师在第i分钟讲述ai个定理.   米什卡真的对微积分很感兴趣,尽管在演讲的所有时间都很难保持清醒.给你一个米什卡行 ...

  2. poj2566尺取变形

    Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...

  3. POJ3061 Subsequence 尺取or二分

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  4. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

  5. POJ:2566-Bound Found(尺取变形好题)

    Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5408 Accepted: 1735 Special J ...

  6. 2018亚洲区预选赛北京赛站网络赛 D.80 Days 尺取

    题面 题意:你带着K元要去n个城市,这n个城市是环形的,你可以选择任意一个起点,然后顺时针走,对于每个城市,到达时可以获得a元,但是从这里离开又需要花费b元,问你能否找到一个起点(输出花钱最少的那个) ...

  7. POJ-3061 Subsequence 二分或尺取

    题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断 ...

  8. Gym 101257G:24(尺取)

    http://codeforces.com/gym/101257/problem/GGym 101257G 题意:给出n个人,和一个数s,接下来给出每个人当前的分数和输掉的概率.当一个人输了之后就会掉 ...

  9. Codeforces - 1191E - Tokitsukaze and Duel - 博弈论 - 尺取

    https://codeforc.es/contest/1191/problem/E 参考自:http://www.mamicode.com/info-detail-2726030.html 和官方题 ...

随机推荐

  1. JavaScript变量类型检测总结

    JavaScript中的变量类型: 基本类型值:Undefined,Null,Boolean,Number和String. 按值访问(可直接操作保存在变量中的变量值): 复制规则:当复制基本类型值时: ...

  2. mysql 生成UUID() 即 ORACLE 中的guid()函数

    MYSQL 生成UUID 即 guid 函数-- 带 - 的UUIDselect UUID() -- 去掉 - 的UUIDselect replace(uuid(),'-','') 一个表的数据插入另 ...

  3. 学习ThinkPHP5的第一天(安装 连接数据库)

    参考文档:thinkPHP5.0完全手册  一.安装 采用的是git安装方式: 应用项目:https://github.com/top-think/think 核心框架:https://github. ...

  4. PHP 好用第三方库

    PHP 好用第三方库 whoops 更好的php错误报告库 [github]:https://github.com/filp/whoops Whoops是一个易于处理和调试错误的PHP库 .它提供基于 ...

  5. STM32F4寄存器编写跑马灯例程

    最近由于在学习STM32看到别人用寄存器编程控制跑马灯,于是自己也想试一试.可是试了好久终究弄不出来.回头看了下库函数的调用关系才搞明白.首先通过查看GPIOA的设置函数发现设置如下: void GP ...

  6. c语言单向链表逆转实现方法

    自己理解的思路如下所示: 从第二个节点开始,先记录下一个节点,把第二个节点移到头节点之前,头节点变为移动的这个节点之前记录的节点变为接下来要移动的节点用for循环重复最后把原来头节点变成尾节点(*ne ...

  7. TCP交互流程

    前言:在FPGA上实现TCP协议实际是一个不太好的计划,因为FPGA最擅长的是流水线处理方式,而TCP存在交互,导致FPGA需要进行反馈式的处理.但是由于项目新增的附加要求而又只能在FPGA上去实现T ...

  8. 201555301 2016-2017-2《Java程序设计》课程总结

    20155301 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:我对师生关系的思考 预备作业2:从现有技能获取的经验应用于JAVA中 预备作业 ...

  9. 20155304 2016-2017-2 《Java程序设计》第十周学习总结

    20155304 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是 ...

  10. 20155306 实验一《Java开发环境的熟悉》实验报告

    实验一 Java开发环境的熟悉(Linux + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1. ...