hiho~


http://hihocoder.com/contest/hiho39/problem/1#

逆序对裸题,水题


#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LowBit(x)  ((x)&(-(x)))

const int MAXN = 100005;

int n,nn;
int a[MAXN] = {0},b[MAXN] ={0};
int sum[MAXN] = {0};
long long ans = 0;

void insert(int x)
{
    while(x <= n)
    {
       sum[x] ++;  x += LowBit(x);  
    }
}
int count(int x)
{
    int ret = 0;
    while(x > 0)
    {
      ret += sum[x]; x-=LowBit(x);
    }
    return ret;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("hiho.in","r",stdin);
    freopen("hiho.out","w",stdout);
#endif

    scanf("%d",&n);
    for(int i = 1; i <= n ;i++)
    {scanf("%d",&a[i]); b[i] = a[i];}

    std::sort(b + 1, b + n + 1);
    nn = std::unique(b + 1,b + n + 1)- (b + 1);

    for(int i = 1; i <= n; i++)
    {
        static int bi;

        bi = std::lower_bound(b + 1, b + nn + 1,a[i]) - b;

        ans += count(n) - count(bi);     insert(bi);
    }

    std::cout << ans;

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

你可能感兴趣的:(hiho,templates)