hiho~ 分类: hiho templates 2015-04-01 17:37 32人阅读 评论(0) 收藏
http://hihocoder.com/contest/hiho39/problem/1#
逆序对裸题,水题
#include<map>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<iostream>
#include<algorithm>
#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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。