Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15247 Accepted Submission(s): 9311
1 /************************************************************************* 2 > File Name: code/hud/1394.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年10月28日 星期三 21时16分47秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #include<cctype> 21 22 #define yn hez111qqz 23 #define j1 cute111qqz 24 #define ms(a,x) memset(a,x,sizeof(a)) 25 using namespace std; 26 const int dx4[4]={1,0,0,-1}; 27 const int dy4[4]={0,-1,1,0}; 28 typedef long long LL; 29 typedef double DB; 30 const int inf = 0x3f3f3f3f; 31 const int N=5E3+7; 32 int c[N]; 33 int n,a[N]; 34 35 int lowbit( int x) 36 { 37 return x&(-x); 38 } 39 40 41 void update ( int x,int delta) 42 { 43 // cout<<"a?"<<endl; 44 for ( int i = x ; i <= n ; i = i + lowbit(i)) c[i] = c[i] + delta; 45 } 46 int Sum( int x) 47 { 48 // cout<<"b?"<<endl; 49 int res = 0 ; 50 for ( int i = x ;i >= 1; i = i - lowbit(i)) 51 { 52 res = res + c[i]; 53 } 54 return res; 55 } 56 int main() 57 { 58 #ifndef ONLINE_JUDGE 59 freopen("in.txt","r",stdin); 60 #endif 61 while (scanf("%d",&n)!=EOF) 62 { 63 ms(c,0); 64 for ( int i = 1 ; i <= n ; i++) 65 { 66 scanf("%d",&a[i]); 67 a[i]++; 68 } 69 int cnt = 0 ; 70 int ans = inf; 71 // puts("mmmmmmmiao?"); 72 for ( int i = 1 ; i <= n ; i++) 73 { 74 update(a[i],1); 75 cnt = cnt + i - Sum(a[i]); 76 // puts("whats wrong?"); 77 } 78 // printf("cnt::%d\n",cnt); 79 if (cnt<ans) ans = cnt; 80 for ( int i = 1 ; i <= n ; i++) 81 { 82 cnt =cnt -a[i]+n-a[i]+1; 83 if (cnt<ans&&cnt>0) ans = cnt; 84 // printf("cnt:%d\n",cnt); 85 } 86 printf("%d\n",ans); 87 } 88 89 90 91 #ifndef ONLINE_JUDGE 92 fclose(stdin); 93 #endif 94 return 0; 95 }
hdu 1394 Minimum Inversion Number (树状数组 逆序对)
原文:http://www.cnblogs.com/111qqz/p/4918837.html