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 ...
随机推荐
- how to setting a i2c driver
How to instantiate I2C devices============================== Unlike PCI or USB devices, I2C devices ...
- 字符串:HDU3064-最长回文
最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descri ...
- 笔记-python-centos环境下安装配置
笔记-python-centos环境下安装配置 1. 准备工作 环境准备 centos6.5 mini,已有python 2.6.6 下载源码包 Python官网下载Gzipped sour ...
- UVa 1630 区间DP Folding
一个字符串如果能简写,要么是重复多次,按题中的要求简写:要么是左右两个部分分别简写后再拼起来. dp(i, j)表示字串(i, j)所能被简写的最短的字符串. 判断一个字符串是否为周期串以及求出它的周 ...
- (转)React 入门实例教程
作者: 阮一峰 现在最热门的前端框架,毫无疑问是 React . 上周,基于 React 的 React Native 发布,结果一天之内,就获得了 5000 颗星,受瞩目程度可见一斑. React ...
- Java多线程框架源码阅读之---ReentrantLock
ReentrantLock基于Sync内部类来完成锁.Sync有两个不同的子类NonfairSync和FairSync.Sync继承于AbstractQueuedSynchronizer. Reent ...
- poj 2195 最小费用最大流模板
/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...
- js版本下拉菜单
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 将模型储存到本地-FastCoder
// // ViewController.m // 模型转data储存 // // Created by 谭启宏 on 16/3/4. // Copyright © 2016年 tqh. All ri ...
- iOS------手势操作(nib文件、纯代码)
总共有六种手势识别:轻击手势(TapGestureRecognizer),轻扫手势 (SwipeGestureRecognizer), 长按手势(LongPressGestureRecognizer) ...