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 ...
随机推荐
- 安装.NET Core遇到的错误
如果验证出现如下错误 Failed to load /opt/dotnet/shared/Microsoft.NETCore.App/1.1.0/libcoreclr.so, error: libun ...
- 下载Eclipse、下载Java各个版本,来这里就对了
Eclipse官网:http://www.eclipse.org/ 不信你去看看 Java官网:https://www.java.com/ 不信你去看看 可惜是,每次进入官网提示都是下面这样的:来,我 ...
- March 01st, 2018 Week 9th Thursday
Let bygones be bygones. 过去的就让它过去吧. What happened has happened, it cannot be undone, so just leave it ...
- Java高级教程02
目录 1.Java线程 1.1. 多线程和多进程 1.2. 线程的执行过程: 1.3. 创建线程的方法 (1). 方法1:通过run() (2). 方法2: 复写Runnable接口(推荐) 1.4. ...
- java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询
1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...
- CORS(跨域资源共享)详解及与JSONP的对比
上篇讲解的JSONP的跨域请求方式,但是在浏览器的支持及请求方式上有局限性,接下来将介绍一种新的跨域请求方式CORS. CORS是一个W3C标准,全称是"跨域资源共享"(Cross ...
- 【Python语言】Python介绍
目前在大数据的行业中有3种语言:1. Java ---> 用于大数据工程2. Scala ---> 用于大数据工程和数据科学3.Python ---> 用于数据科学 Python是一 ...
- python之常用模块(续)
time模块 random模块 sys模块 os模块 序列化模块 time模块 有三种方式表示 在Python中,通常有三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串 ...
- Web开发人员vs网页设计师
Web开发人员vs网页设计师 我们都遇到过,但实际的区别是什么?如果您是该领域的新手,请阅读详细内容,这些内容比您想象的更重要. 经过几周(或几个月)的规划和准备,进行市场调查,与其他企业家交谈,现在 ...
- Cglib动态代理实现方式
Cglib动态代理实现方式 我们先通过一个demo看一下Cglib是如何实现动态代理的. 首先定义个服务类,有两个方法并且其中一个方法用final来修饰. public class PersonSer ...