标签:des style blog http io ar color os sp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11788 Accepted Submission(s): 7235
1 #include<cstdio> 2 #include<algorithm> 3 4 using namespace std; 5 6 #define lson l , m , rt << 1 7 #define rson m + 1 , r , rt << 1 | 1 8 9 const int maxn = 5000 + 311; 10 int sum[maxn<<2]; 11 12 13 void PushUp(int rt) 14 { 15 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; 16 } 17 18 void build(int l,int r,int rt) 19 { 20 int m; 21 22 sum[rt]=0; 23 if(l==r) { 24 return; 25 } 26 m=(l+r)>>1; 27 build(lson); 28 build(rson); 29 } 30 31 int query(int L,int R, int l, int r,int rt) 32 { 33 int m,ret; 34 35 if(L<=l && r<=R) { 36 return sum[rt]; 37 } 38 39 m=(l+r) >> 1; 40 ret=0; 41 if(L<=m) ret+=query(L,R,lson); 42 if(R>m) ret+=query(L,R,rson); 43 44 return ret; 45 } 46 47 void Updata(int p,int l,int r,int rt) 48 { 49 int m; 50 if (l==r) { 51 sum[rt]++; 52 return ; 53 } 54 m=(l+r)>>1; 55 if(p<=m) Updata(p,lson); 56 else Updata(p,rson); 57 58 PushUp(rt); 59 } 60 61 int main() 62 { 63 int n,sum,x[maxn]; 64 65 while(~scanf("%d",&n)) { 66 sum=0; 67 build(0,n-1,1); 68 69 for(int i = 0;i < n;i++) { 70 scanf("%d",&x[i]); 71 sum+=query(x[i],n-1,0,n-1,1); 72 Updata(x[i],0,n-1,1); 73 } 74 int ret=sum; 75 for(int i=0;i<n;i++) { 76 sum+=n-x[i]-x[i]-1; 77 ret=min(ret,sum); 78 } 79 80 printf("%d\n",ret); 81 } 82 83 return 0; 84 }
[HDU] 1394 Minimum Inversion Number [线段树求逆序数]
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/sxiszero/p/4125847.html