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(1≤lrN). 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

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 7619⁄356 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

官方题解:

题解前半段很好懂,最后一段说可以使用树状数组,一开始不是很懂,看了别人的题解发现原来可以用求逆序对数的做法。

我一开始想到的是求[i+1,n]区间大于等于f[i]的个数,于是就用归并树做了。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
#include<set>
#include<stack>
#define ll long long
#define pb push_back
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define cls(name,x) memset(name,x,sizeof(name))
using namespace std;
const int inf=1e9+10;
const ll llinf=1e16+10;
const int maxn=1e6+10;
const int maxm=2e5+10;
const int mod=1e9+7;
const double pi=acos(-1.0);
int n;
ll sum,k;
ll f[maxn];
struct node
{
    ll *num;
    int len;
}tree[maxn*4];
void build(int l,int r,int rt)
{
    tree[rt].len=r-l+1;
    tree[rt].num=new ll[tree[rt].len];
    if(l==r)
    {
        tree[rt].num[0]=f[l];
        return ;
    }
    int mid=(l+r)/2;
    build(l,mid,rt*2);
    build(mid+1,r,rt*2+1);
    int i=0,j=0,k=0;
    while(i<tree[rt*2].len&&j<tree[rt*2+1].len)
    {
        if(tree[rt*2].num[i]<tree[rt*2+1].num[j])
            tree[rt].num[k++]=tree[rt*2].num[i++];
        else
            tree[rt].num[k++]=tree[rt*2+1].num[j++];
    }
    while(i<tree[rt*2].len)
        tree[rt].num[k++]=tree[rt*2].num[i++];
    while(j<tree[rt*2+1].len)
        tree[rt].num[k++]=tree[rt*2+1].num[j++];
}
int querry(int ql,int qr,ll key,int l,int r,int rt)
{
    if(ql==l&&qr==r)
    {
        int c=lower_bound(tree[rt].num,tree[rt].num+tree[rt].len,key)-tree[rt].num;
        return tree[rt].len-c;
    }
    int mid=(l+r)/2;
    if(qr<=mid)
        return querry(ql,qr,key,l,mid,rt*2);
    else if(ql>=mid+1)
        return querry(ql,qr,key,mid+1,r,rt*2+1);
    else
        return querry(ql,mid,key,l,mid,rt*2)+querry(mid+1,qr,key,mid+1,r,rt*2+1);
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d %lld",&n,&k))
    {
        sum=0;
        f[0]=sum-k*(0+1);
        for(int i=1;i<=n;i++)
        {
            int t;
            scanf("%d",&t);
            sum=sum+t;
            f[i]=sum-k*(i+1);
        }
        build(1,n,1);
        ll ans=0;
        for(int i=0;i<=n-1;i++)
        {
            ans+=querry(i+1,n,f[i],1,n,1);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

 
  

转载于:https://www.cnblogs.com/mgz-/p/7128531.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值