标签:
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #define MAXN 100000 6 using namespace std; 7 int segtree[MAXN*4],a[MAXN*4]; 8 int n,sum; 9 void adddata(int now) 10 { 11 segtree[now]=segtree[(now<<1)]+segtree[(now<<1)+1]; 12 } 13 void buildtree(int now,int l,int r) 14 { 15 segtree[now]=0; 16 if (l==r) return; 17 int mid=(l+r)>>1; 18 buildtree((now<<1),l,mid); 19 buildtree((now<<1)+1,mid+1,r); 20 adddata(now); 21 } 22 int query(int now,int l,int r,int begin,int end) 23 { 24 if (begin<=l && end>=r) return segtree[now]; 25 int mid=(l+r)>>1,ans=0; 26 if (begin<=mid) ans+=query((now<<1),l,mid,begin,end); 27 if (end>mid) ans+=query((now<<1)+1,mid+1,r,begin,end); 28 return ans; 29 } 30 void pointchange(int now,int l,int r,int x,int v) 31 { 32 if (l==r) {segtree[now]+=v; return;} 33 int mid=(l+r)>>1; 34 if (x<=mid) pointchange((now<<1),l,mid,x,v); 35 else pointchange((now<<1)+1,mid+1,r,x,v); 36 adddata(now); 37 } 38 int main() 39 { 40 int i; 41 int minn; 42 while (~scanf("%d",&n)) 43 { 44 buildtree(1,0,n-1); 45 sum=0; 46 for (i=0;i<n;i++) 47 { 48 scanf("%d",&a[i]); 49 sum+=query(1,0,n-1,a[i],n-1 ); 50 pointchange(1,0,n-1,a[i],1); 51 } 52 minn=sum; 53 for (i=0;i<n;i++) 54 { 55 sum+=n-2*a[i]-1; 56 minn=min(minn,sum); 57 } 58 printf("%d\n",minn); 59 } 60 return 0; 61 }
【hdu1394】Minimum Inversion Number
标签:
原文地址:http://www.cnblogs.com/DMoon/p/5096650.html