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
#include
#include
#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=0;--i){
            sum+=(ll)getsum(a[i]-1)*b[i];
            modify(a[i],1);
        }
        printf("%lld\n",sum);
    }
    return 0;
}



你可能感兴趣的:(#,数据结构)