您的位置:首页 > 产品设计 > UI/UE

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

2014-08-28 20:22 141 查看


Number sequence

My Tags (Edit)
Source : SCU Programming
Contest 2006 Final
Time limit : 1 secMemory 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: