题意:求最小逆序数
#includeusing namespace std;int c[5005],n,a[5005];int lowbit(int x){ return x&(-x);}int sum(int x){ int sum=0; while(x>0) { sum+=c[x]; x-=lowbit(x); } return sum;}void inster(int x,int i){ while(x<=n) { c[x]+=i; x+=lowbit(x); }}int main(){ int i,s[5005],count,min; while(scanf("%d",&n)>0) { memset(a,0,sizeof(a)); memset(c,0,sizeof(c)); count=0; for(i=1;i<=n;i++) { scanf("%d",&a[i]); a[i]++; inster(a[i],1); count+=i-sum(a[i]); } s[1]=count; for(i=n;i>=2;i--) { count=count+2*a[i]-n-1; //把第n个数移到第一位,所以,从i=n开始;那么,要减去在它前面比它大的逆序数的个数,还要加上当它排第一位时,在它后面比它小的数的个数 s[i]=count; } min=s[1]; for(i=2;i<=n;i++) if(min>s[i]) min=s[i]; printf("%d\n",min); } return 0;}