标签:
3 1 2 3 4 4 3 2 1
0 6
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> using namespace std; const int N = 1001; int n; int num[N]; int c[N]; struct node{ int x; int id; }q[N] ; bool cmp(node a,node b){ return a.x < b.x; } int lowbit(int x){ return x&(-x); } int getsum(int x){ int s = 0; while(x>0){ s += c[x]; x -= lowbit(x); } return s; } void add(int x,int y){ while(x<=n){ c[x] += y; x += lowbit(x); } } int main() { while(scanf("%d",&n)!=EOF){ memset(c,0,sizeof(c)); memset(num,0,sizeof(num)); for(int i=1;i<=n;i++){ scanf("%d",&q[i].x); q[i].id = i; } sort(q+1,q+1+n,cmp); for(int i=1;i<=n;i++){ num[q[i].id] = i; } int sum = 0; for(int i=1;i<=n;i++){ add(num[i],1); sum += getsum(n) - getsum(num[i]); } printf("%d\n",sum); } return 0; }
版权声明:本文为博主原创文章,如有特殊需要请与博主联系 QQ : 793977586。
标签:
原文地址:http://blog.csdn.net/yeguxin/article/details/47782549