首页 技术 正文
技术 2022年11月14日
0 收藏 395 点赞 4,738 浏览 1492 个字

题目描述

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 is given from Standard Input in the following format:
N K
a1
a2
:
aN

输出

Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K.

样例输入

3 6
7
5
7

样例输出

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.

首先对于所有数减去k,这样就不用除(r-l+1), 然后我们发现所求的就是有多少对l,r,使得sum[r]-sum[l-1] >= 0, sum是减去k之后的序列的前缀和

用树状数组对sum求有多少个顺序对,多加一个0这个数,代表从sum[r]-sum[0]对答案的贡献。

由于sum[i]可能很大,所以需要离散化 用unique

#include <bits/stdc++.h>
#define ll long long
const int mod=1e9+;
const int maxn=2e5+;
using namespace std;
//int s[maxn];
ll sum[maxn],t[maxn];
ll tree[maxn];
int nn,m;
void add(int x)
{
while(x<=nn)//nn为上限,要大于离散化后的最大的数字,
{
tree[x]++;
x+=x&-x;//向上更新,树状数组核心代码。此处就不解释了,不会的先学树状数组。
}
}
ll query(int x)
{
ll num=;
while(x)
{
num+=tree[x];
x-=x&-x;//向下求和
}
return num;
}
int main()
{
int n,k,a;
scanf("%d %d",&n,&k);
nn=n+;
for(int i=;i<=n;i++){
scanf("%d",&a);
sum[i]=t[i]=sum[i-]+a-k;
}
sort(t,t+n+);
m=unique(t,t+n+)-t;
for(int i=;i<=n;i++){
sum[i]=lower_bound(t,t+m+,sum[i])-t+;
}
ll ans=;
for(int i=;i<=n;i++){
ans+=query(sum[i]);
add(sum[i]);
}
printf("%lld\n",ans);
return ;
}

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,030
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,859