您的位置:首页 > 其它

hiho~ 分类: hiho templates 2015-04-01 17:37 32人阅读 评论(0) 收藏

2015-04-01 17:37 375 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐