HOJ 2275——Number sequence(树状数组)

Number sequence

My Tags  (Edit)
  Source : SCU Programming Contest 2006 Final
  Time limit : 1 sec   Memory limit : 64 M

Submitted : 1540, Accepted : 417

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


——————————————————分割线——————————————————

题目大意:

给定n个数,求满足Ai < Aj > Ak and i < j < k.的总的组合数


思路:

对于第i个数,顺序统计比它小的数的个数a,再逆序统计比它小的个数b,就能得到左右两边比它小的数的个数,那么对于第i个数的方案数就是a*b


还有这个题目没说EOF结束的,但是如果没有EOF就错了,错了好几发~~~


#include<iostream>
#include<cstring>
#include<cstdio>
#define Local
#define M 50000+10
#define maxn 32768
#define ll long long
using namespace std;
int a[M],b[M],c[M];
void modify(int x,int v)
{
    for(int i=x;i<=M;i+=i&-i){
        c[i]+=v;
    }
}
int getsum(int x)
{
    int sum=0;
    for(int i=x;i>0;i-=i&-i){
        sum+=c[i];
    }
    return sum;
}
int main()
{
#ifdef local
    freopen("in.txt","r",stdin);
    freopen("ou.txt","w",stdout);
#endif // local

    int n;
    while(scanf("%d",&n)!=EOF){
        memset(c,0,sizeof(c));
        ll sum=0;
        for(int i=0;i<n;++i){
            scanf("%d",&a[i]);
            a[i]++;
            b[i]=getsum(a[i]-1);
            modify(a[i],1);
        }
        memset(c,0,sizeof(c));
        for(int i=n-1;i>=0;--i){
            sum+=(ll)getsum(a[i]-1)*b[i];
            modify(a[i],1);
        }
        printf("%lld\n",sum);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值