AtCoder - 2581 Meaningful Mean
Problem Statement
You are given an integer sequence of length N, a= {a1,a2,…,aN}, and an integer K.
a has N(N+1)⁄2 non-empty contiguous subsequences, {al,al+1,…,ar} (1≤l≤r≤N). Among them, how many have an arithmetic mean that is greater than or equal to K?
Constraints
- All input values are integers.
- 1≤N≤2×105
- 1≤K≤109
- 1≤ai≤109
Input
Input is given from Standard Input in the following format:
N K
a1
a2
:
aN
Output
Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K.
Sample Input 1
3 6
7
5
7
Sample Output 1
5
All the non-empty contiguous subsequences of a are listed below:
- {a1} = {7}
- {a1,a2} = {7,5}
- {a1,a2,a3} = {7,5,7}
- {a2} = {5}
- {a2,a3} = {5,7}
- {a3} = {7}
Their means are 7, 6, 19⁄3, 5, 6 and 7, respectively, and five among them are 6or greater. Note that {a1} and {a3} are indistinguishable by the values of their elements, but we count them individually.
Sample Input 2
1 2
1
Sample Output 2
0
Sample Input 3
7 26
10
20
30
40
30
20
10
Sample Output 3
13
树状数组sb题。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=200005;
ll num[maxn],a[maxn],ans;
int n,r[maxn],f[maxn],k,ky;
inline int read(){
int x=0; char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return x;
}
inline void update(int x,int y){ for(;x<=ky;x+=x&-x) f[x]+=y;}
inline int query(int x){ int an=0; for(;x;x-=x&-x) an+=f[x]; return an;}
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) a[i]=read()-k;
for(int i=1;i<=n;i++){
a[i]+=a[i-1],num[i]=a[i];
if(a[i]>=0) ans++;
}
sort(num+1,num+n+1);
ky=unique(num+1,num+n+1)-num-1;
for(int i=1;i<=n;i++) r[i]=lower_bound(num+1,num+ky+1,a[i])-num;
for(int i=1;i<=n;i++) ans+=(ll)query(r[i]),update(r[i],1);
printf("%lld\n",ans);
return 0;
}
AtCoder - 2581 Meaningful Mean的更多相关文章
- Atcoder E - Meaningful Mean(线段树+思维)
题目链接:http://arc075.contest.atcoder.jp/tasks/arc075_c 题意:问数组a有多少子区间平均值为k 题解:一开始考虑过dp,但是显然不可行,其实将每一个数都 ...
- AtCoder - 2581 树状数组
You are given an integer sequence of length N, a= {a1,a2,…,aN}, and an integer K. a has N(N+1)⁄2 non ...
- AtCoder Regular Contest 075 E - Meaningful Mean(树状数组)
题目大意:求一个数组中,平均值不小于k的连续子序列个数 所有数减去k,算个前缀和出来,就变成二维数点问题了. 没有修改,离线的话就是CZL所说的“NOIP最喜欢的套路”了:倒着加进BIT,以权值为数组 ...
- clean code meaningful names
---恢复内容开始--- Meaningful Names: use Intention-Revealing Names //nice,Everyone who reads your code (in ...
- 有意义的命名 Meaningful names
名副其实 use intention-revealing names 变量.函数或类的名称应该已经答复了所有的大问题.它该告诉你,他为什么会存在,他做什么事,应该怎么用.我们应该选择都是致命了计量对象 ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识
链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...
- Clean Code – Chapter 2: Meaningful Names
Use Intention-Revealing Names The name should tell you why it exists, what it does, and how it is us ...
- AtCoder Regular Contest 082
我都出了F了……结果并没有出E……atcoder让我差4分上橙是啥意思啊…… C - Together 题意:把每个数加1或减1或不变求最大众数. #include<cstdio> #in ...
随机推荐
- java web项目(spring项目)中集成webservice ,实现对外开放接口
什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...
- List删除元素
在单线程环境下的解决办法 public void remove() { if (lastRet == -1) throw new IllegalStateException(); checkForCo ...
- loj2275 「JXOI2017」颜色
枚举右端点,然后看左端点合法情况. 先预处理每个颜色 \(i\) 的最大出现位置 \(max_i\) 和最小出现位置 \(min_i\).对于枚举右端点在一个位置 \(i\),凡是 \(max_k & ...
- tomcat6-endpoint设计
之前写的一个ppt 搬到博客来
- TensorFlow——小练习:counter
下面的例子演示了如何使用变量实现一个 简单的计数器(counter) # _*_coding:utf-8_*_ import tensorflow as tf import numpy as np # ...
- maven项目打包jar,含有依赖jar
在pom文件中添加一下插件 <plugin> <artifactId>maven-assembly-plugin</artifactId> <configur ...
- rabbitmq exchange type
This is the fourth installment to the series: RabbitMQ for Windows. In thelast installment, we revi ...
- 大数相减 C语言
#include <stdio.h> #include <string.h> using namespace std; ],b[]; void Sub() { ; if(a = ...
- IDA 操作记录
IDA 操作记录 1.加载文件 windows 下,用ida加载文件后,会在该文件同目录下生成几个文件,含义如下: .id0 : 二叉树数据库 .id1: 文件包含描述每个程序字节的标记 .na ...
- Mysql 数值类型
Mysql数值类型 整数型 小数型(浮点数) 日期时间型