AtCoder Regular Contest 075 E
来源:互联网 发布:国外创意广告视频 知乎 编辑:程序博客网 时间:2024/06/05 21:53
Meaningful Mean
Time limit : 2sec / Memory limit : 256MB
Score : 600 points
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 Ka1a2: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 6757
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 6 or greater. Note that {a1} and {a3} are indistinguishable by the values of their elements, but we count them individually.
Sample Input 2
1 21
Sample Output 2
0
Sample Input 3
7 2610203040302010
Sample Output 3
13
为了处理方便将每个数减去k,省略了除以(r-l+1),然后计算前缀和,因为一对前缀和差大于0即可保证平均值大于k,即sum[r]>=sum[l],
将前缀和存储排序,利用树状数组求逆序对
#include<bits/stdc++.h>using namespace std;const int N = 2e5+10;typedef long long LL;LL a[N], sum[N];LL b[N], c[N], d[N];vector<LL>v;LL lowbit(LL k){ return k&-k;}void add(LL x){ while(x<N) { c[x]+=1; x+=lowbit(x); } return ;}LL ss(LL x){ LL ans=0; while(x>0) { ans+=c[x]; x-=lowbit(x); } return ans;}int main(){ int n; LL k; scanf("%d %lld", &n, &k); for(int i=1;i<=n;i++) { scanf("%lld", &a[i]); a[i]-=k; sum[i]=sum[i-1]+a[i]; } v.clear(); for(int i=0;i<=n;i++) { v.push_back(sum[i]); } sort(v.begin(),v.end()); v.resize(unique(v.begin(),v.end())-v.begin()); for(int i=0;i<=n;i++) { b[i]=(lower_bound(v.begin(),v.end(),sum[i])-v.begin())+1; } LL ans=0; for(int i=0;i<=n;i++) { ans+=ss(b[i]); add(b[i]); } cout<<ans<<endl; return 0;}
- AtCoder Regular Contest 075 E
- 【树状数组】AtCoder Regular Contest 075 E
- AtCoder Regular Contest 077 E
- AtCoder Regular Contest 077 E
- AtCoder Regular Contest 079-E
- AtCoder Regular Contest 080 E
- AtCoder Regular Contest 080 E
- AtCoder regular contest 081 E
- 【Atcoder】Regular Contest 079 D E
- AtCoder Regular Contest 079 C D E
- AtCoder Regular Contest 082-E-ConvexScore
- Atcoder Regular Contest 072 E Alice in Linear Land
- AtCoder Regular Contest 077
- AtCoder Regular Contest 077
- AtCoder Regular Contest 078
- AtCoder Regular Contest 079
- Atcoder Regular Contest 084
- AtCoder Regular Contest 086
- 剑指offer--栈的压入、弹出序列
- 分工明确,知人善用
- Python 切片
- 编码的故事
- Makefile通用版本
- AtCoder Regular Contest 075 E
- 微信小程序入门
- Python 迭代
- 大数A*B
- 《深入理解java虚拟机》学习笔记(2)——java虚拟机运行时数据区域
- Pixhawk精准着陆之IRLock配置
- PHP_CodeSniffer 使用攻略
- Python 列表生成式
- 一致性hash算法