标签:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 const int maxn=5010; 6 int sum[maxn<<2]; 7 void push_up(int rt) 8 { 9 sum[rt]=sum[rt<<1]+sum[rt<<1 | 1]; 10 } 11 void build(int rt,int first,int end) 12 { 13 sum[rt]=0; 14 if(first==end) 15 return ; 16 int mid=(first+end)>>1; 17 build(rt<<1,first,mid); 18 build(rt<<1 | 1,mid+1,end); 19 } 20 int quary(int rt,int first,int end,int left,int right) 21 { 22 if(left<=first && right>=end) 23 return sum[rt]; 24 int mid=(first+end)>>1; 25 int ans=0; 26 if(left<=mid) 27 ans+=quary(rt<<1,first,mid,left,right); 28 if(right>mid) 29 ans+=quary(rt<<1 | 1,mid+1,end,left,right); 30 return ans; 31 } 32 void update(int rt,int first,int end,int a) 33 { 34 if(first==end) 35 { 36 sum[rt]++; 37 return ; 38 } 39 int mid=(first+end)>>1; 40 if(a<=mid) 41 update(rt<<1,first,mid,a); 42 else 43 update(rt<<1 | 1,mid+1,end,a); 44 push_up(rt); 45 } 46 int a[maxn]; 47 int main() 48 { 49 int n; 50 while(cin>>n) 51 { 52 build(1,0,n-1); 53 int sum=0; 54 for(int i=0;i<n;i++) 55 { 56 cin>>a[i]; 57 sum+=quary(1,0,n-1,a[i],n-1); 58 update(1,0,n-1,a[i]); 59 } 60 int ans=sum; 61 for(int i=0;i<n;i++) 62 { 63 sum+=n-a[i]-a[i]-1; 64 ans=min(ans,sum); 65 } 66 cout<<ans<<endl; 67 } 68 return 0; 69 }
hdu 1394 Minimum Inversion Number 线段树
标签:
原文地址:http://www.cnblogs.com/cxbky/p/4887726.html