【树状数组】AtCoder Regular Contest 075 E - Meaningful Mean

E - 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 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

Copy
3 6
7
5
7

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 2
1

Sample Output 2

Copy
0

Sample Input 3

Copy
7 26
10
20
30
40
30
20
10

Sample Output 3

Copy
13

Submit


题意:给出一个N个数的序列A,问有多少个区间的平均数大于等于P;

思路:每个数都减去平均数,然后对每个数求前i项和;对前n项和,进行离散化,然后利用树状数组求出对于每个i,前面有多少个数字比它小,然后累加;不过要注意对前n项和遍历一遍,遇到值大于等于0的值ans+1;

样例: 7    5    4    6     8

利用树状数组求的是:    对于5 : 区间(5,5);

                                        对于4 : 区间(5,4)、(4,4)

                                        对于6 : 区间(5,6)、(4,6)、(6,6)

                                        对于8 : 区间(5,8)、(4,8)、(6,8)的区间和是否大于0(各数已经减过p了);

遍历前n项和求的是:(7,7),(7,5),(7,4),(7,6),(7,8)各区间的和是够大于0(各数已经减过p了);

这样就把所有的区间都给处理到了;

样例:

              3    6

              7    5    7

-k:       1   -1    1

sum:   1    0     1

离散:   2    1    3


代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=200005;
typedef long long ll;

struct node
{
    ll index,value;
}a[maxn];
ll C[maxn],n,k;

//离散化排序;
int cmp1(node a,node b)
{
    if(a.value!=b.value)
        return a.value<b.value;
    return a.index<b.index;
}

int cmp2(node a,node b)
{
    if(a.index!=b.index)
        return a.index<b.index;
    return a.value<b.value;
}

ll lowbit(ll x)
{
    return x&(-x);
}

void add(ll x,ll d)
{
    while(x<maxn)
    {
        C[x]+=d;
        x+=lowbit(x);
    }
}

ll sum(ll x)
{
    ll ret=0;
    while(x>0)
    {
        ret+=C[x];
        x-=lowbit(x);
    }
    return ret;
}

int main()
{
    ll ans=0;
    scanf("%lld%lld",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i].value);
        a[i].index=i;
        a[i].value-=k;
    }
    for(int i=2;i<=n;i++)
        a[i].value=a[i-1].value+a[i].value;
    for(int i=1;i<=n;i++)
        if(a[i].value>=0)
            ans+=1;
//printf("%d\n",ans);
    sort(a+1,a+1+n,cmp1);
    for(int i=1;i<=n;i++)
        a[i].value=i;
    sort(a+1,a+1+n,cmp2);
//for(int i=1;i<=n;i++)
//    printf("%d ",a[i].value);

//    printf("\n");
    for(int i=1;i<=n;i++)
    {
        ll x=a[i].value;
        ans+=sum(x);
        add(x,1);
    }
    printf("%lld\n",ans);

    return 0;
}


  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值