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 ...
随机推荐
- 在ubuntu18.04上安装EOS
在ubuntu18.04上安装EOS 在ubuntu18.04上安装EOS的目的: 把交易所的eos转到eos主网,防止交易所跑路或者交易所被黑客攻击 在不联网的安全环境下,用eos官方的命令行工具, ...
- f.lux 自动调节显示器色温
我的环境 f.lux 我的使用感受是让屏幕看起来舒服一些,因为我有近视,所以需要保护眼睛. f.lux官网:https://justgetflux.com/ f.lux v4.47 windows 1 ...
- Linux 小知识翻译 - 「编译器和解释器」
这次聊聊「编译器和解释器」. 编程语言中,有以C为代表的编译型语言和以Perl为代表的解释型语言.不管是哪种,程序都是以人类能够理解的形式记录的,这种形式计算机是无法理解的. 因此,才会有编译器和解释 ...
- Metasploit渗透测试实际应用
Metasploit:如何在 Metasploit 中使用反弹 Shell https://xz.aliyun.com/t/2380 Metasploit:如何使用 msfvenom https:// ...
- JavaScript数据类型之文本类型
引言 字符串(string)是一组由16位值组成的不可变的有序序列,每个字符通常来自于Unicode字符集.JavaScript通过字符串类型来表示文本.字符串的长度(length)是其所含16位值的 ...
- js证书批量生成与打包下载
前边有提到最近的一个证书生成保存下载打印的需求. 之前实现的是一个单个操作的页面,现在把实现的批量效果和进度效果的代码展示出来. html <button class="btn btn ...
- gear gym 思维题
题目:https://vj.69fa.cn/1fc993e7e0e1e6fa7ce4640b8d46ef8d?v=1552762626 这个题目,之前有一点思路,但是呢,后来又不知道怎么去执行,然后就 ...
- P3399 丝绸之路 dp
题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚.西亚和欧洲,将那里的香 ...
- python nt, bool, str 试题
# 1.有变量量name = "aleX leNb" 完成如下操作:name = "aleX leNb"# 1)移除 name 变量对应的值两边的空格,并输出处 ...
- request.getParameter("name")乱码问题
1 页面提交http的get请求时,找到页面的字符编码<%@ page contentType="text/html;charset=utf-8" language=&quo ...