Average Sleep Time CodeForces - 808B (前缀和)
It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, one week in Berland lasts k days!
When Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept records of sleep times for the last n days. So now he has a sequence a1, a2, ..., an, where ai is the sleep time on the i-th day.
The number of records is so large that Polycarp is unable to calculate the average value by himself. Thus he is asking you to help him with the calculations. To get the average Polycarp is going to consider k consecutive days as a week. So there will be n - k + 1 weeks to take into consideration. For example, if k = 2, n = 3 and a = [3, 4, 7], then the result is .
You should write a program which will calculate average sleep times of Polycarp over all weeks.
Input
The first line contains two integer numbers n and k (1 ≤ k ≤ n ≤ 2·105).
The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).
Output
Output average sleeping time over all weeks.
The answer is considered to be correct if its absolute or relative error does not exceed 10 - 6. In particular, it is enough to output real number with at least 6 digits after the decimal point.
Examples
3 2
3 4 7
9.0000000000
1 1
10
10.0000000000
8 2
1 2 4 100000 123 456 789 1
28964.2857142857
Note
In the third example there are n - k + 1 = 7 weeks, so the answer is sums of all weeks divided by 7.
题目链接:CodeForces - 808B
题意:给你一个含有N个数的数组,一个长度K,
让你求从1~k,到n-k+1~n这n-k+1个区间的平均值。
思路:裸的前缀和。
我的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(ll* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll sum[maxn];
ll a[maxn];
ll n,k;
int main()
{
gg(n);
gg(k);
repd(i,,n)
{
gg(a[i]);
sum[i]=sum[i-]+a[i];
}
ll cnt=0ll;
repd(i,k,n)
{
cnt+=(sum[i]-sum[i-k]);
}
double ans=1.000*cnt/(1.0*(n-k+));
printf("%.10lf\n",ans );
return ;
} inline void getInt(ll* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Average Sleep Time CodeForces - 808B (前缀和)的更多相关文章
- Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinat ...
- Codeforces 612D 前缀和处理区间问题
传送门:http://codeforces.com/problemset/problem/612/D (转载请注明出处谢谢) 题意: 给出数字n和k,n表示接下来将输入n个在x轴上的闭区间[li,ri ...
- CodeForces 816B 前缀和
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Dasha and Photos CodeForces - 761F (前缀优化)
大意: 给定n*m初始字符矩阵, 有k个新矩阵, 第$i$个矩阵是由初始矩阵区间赋值得到的, 求选择一个新矩阵, 使得其余新矩阵到它距离和最小. 字符集比较小, 可以考虑每次区间覆盖对每个字符的贡献. ...
- Codeforces 975 前缀和二分算存活人数 思维离直线速度相同判平行
A /* Huyyt */ #include <bits/stdc++.h> using namespace std; typedef long long ll; ]; ]; map< ...
- Vasya And The Mushrooms CodeForces - 1016C (前缀和模拟)
大意: 给定2*n的矩阵, 每个格子有权值, 走到一个格子的贡献为之前走的步数*权值, 每个格子只能走一次, 求走完所有格子最大贡献. 沙茶模拟打了一个小时总算打出来了 #include <io ...
- 看懂SqlServer查询计划【转】
原文链接:http://www.cnblogs.com/fish-li/archive/2011/06/06/2073626.html 开始 SQL Server 查找记录的方法 SQL Server ...
- 【转载】看懂SqlServer查询计划
看懂SqlServer查询计划 阅读目录 开始 SQL Server 查找记录的方法 SQL Server Join 方式 更具体执行过程 索引统计信息:查询计划的选择依据 优化视图查询 推荐阅读-M ...
- 看懂SqlServer查询计划
看懂SqlServer查询计划 阅读目录 开始 SQL Server 查找记录的方法 SQL Server Join 方式 更具体执行过程 索引统计信息:查询计划的选择依据 优化视图查询 推荐阅读-M ...
随机推荐
- 关于C#List中FindAll用法的一些简单示例
using System; using System.Collections.Generic; public partial class List : System.Web.UI.Page { pro ...
- IEEE754浮点数的表示方法
https://blog.csdn.net/K346K346/article/details/50487127
- 英语进阶系列-A01-再别康桥
每天必读知识 音标 发音网页 人称代词与物主代词 时态 朗读50遍词汇系列1 Number word 1 be 2 have 3 get 4 give 5 take 诗歌欣赏 [原诗] [英文版] 再 ...
- P2066 机器分配 DP
题目描述 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M≤15,N≤10.分 ...
- 在Intellij IDEA下通过Hibernate逆向生成实体类
前言:在IDEA中,通过相关插件,可以利用Hibernate逆向生成数据表对应的实体类.具体操作及注意事项见本篇随笔. 1.创建一个基于maven的hibernate工程.并在工程中添夹hiberna ...
- 【NOI2008】志愿者招募
[NOI2008]志愿者招募 和[2017山东day7]养猫做法类似. 都是神仙题. 首先我设\(c_{i,j}=[l[j]\leq i\leq r[j]]\) ,于是就可以列出下面的不等式: \[ ...
- ASP.NET 应用程序遭遇Server Application Unavailable问题的解决的方法
公司服务器有.NET2的应用在执行,而我使用了.NET4平台开发,本机測试没问题,扔服务器发现要么我的新平台不好使,要么.NET2的旧平台不好使,各种重新启动IIS服务和WWW服务都无济于事 当我意识 ...
- 前端使用 Nginx 反向代理彻底解决跨域问题
引入网址https://blog.csdn.net/larger5/article/details/81286324 1.请求后端数据失败 代码: <!DOCTYPE html> < ...
- 15 python 初学(闭包,函数装饰器)
这一部分很重要,一定要透彻理解.可参考大神博客: http://www.cnblogs.com/yuanchenqi/articles/5830025.html 闭包: 如果在一个内部函数里,对在外 ...
- linux命令之vmstat
vmstat 参数 功能:报告虚拟内存.swap.io.上下文和 CPU 统计信息. 分析了这些文件: /proc/meminfo /proc/stat /proc/*/stat 常用选项: -a 打 ...