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 Na= {a1,a2,…,aN}, and an integer K.

a has N(N+1)2 non-empty contiguous subsequences, {al,al+1,…,ar(1lrN). Among them, how many have an arithmetic mean that is greater than or equal to K?

Constraints

  • All input values are integers.
  • 1N2×105
  • 1K109
  • 1ai109

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

Copy
3 6757

Sample Output 1

Copy
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 7619356 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

Copy
1 21

Sample Output 2

Copy
0

Sample Input 3

Copy
7 2610203040302010

Sample Output 3

Copy
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;}






原创粉丝点击