hoj 2275 Number sequence

Number sequence

Given a number sequence which has N element(s), please calculate the number of different collocation for three number Ai, Aj, Ak, which satisfy that Ai < Aj > Ak and i < j < k.


Input


The first line is an integer N (N <= 50000). The second line contains N integer(s): A1, A2, ..., An(0 <= Ai <= 32768).


Output

There is only one number, which is the the number of different collocation.


Sample Input


5
1 2 3 4 1


Sample Output


6


题目就是统计序列中Ai < Aj > Ak(i < j < k)的个数,可以从前往后统计每个元素之前小于它的数的个数,在从后往前统计每个元素之后小于它的数的个数。然后乘积加和即可。用树状数组轻松搞定!!


AC代码如下:


#include<iostream>
#include<cstdio>
#include<cstring>
#define M 50010
using namespace std;

int c[M],num[M];
int l[M],n;

int lowbit(int a)
{
    return a&-a;
}

void add(int a,int b)
{
    while (a<M)
    {
        c[a]+=b;
        a+=lowbit (a);
    }
}

int sum(int a)
{
    int ans=0;
    while(a>0)
    {
        ans+=c[a];
        a-=lowbit(a);
    }
    return ans;
}

int main ()
{
    int i,j;
    int a,b;
    while(~scanf("%d",&n))
    {
        memset(c,0,sizeof c);
        memset(num,0,sizeof num);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            l[i]=sum(num[i]-1);
            add(num[i],1);
        }
        memset(c,0,sizeof c);
        long long ans=0;
        for(i=n;i>=1;i--)
        {
            ans=ans+(long long)sum(num[i]-1)*l[i];
            add(num[i],1);
        }
        printf("%lld\n",ans);
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值