cf C. Insertion Sort
http://codeforces.com/contest/362/problem/C
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int a[5010]; 7 int c[50010]; 8 int n; 9 10 int lowbit(int x) 11 { 12 return x&(-x); 13 } 14 15 void add(int x) 16 { 17 while(x>0) 18 { 19 c[x]++; 20 x-=lowbit(x); 21 } 22 } 23 24 int sum(int x) 25 { 26 int sum1=0; 27 while(x<=n) 28 { 29 sum1+=c[x]; 30 x+=lowbit(x); 31 } 32 return sum1; 33 } 34 35 int main() 36 { 37 while(scanf("%d",&n)!=EOF) 38 { 39 for(int i=1; i<=n; i++) 40 { 41 scanf("%d",&a[i]); 42 a[i]++; 43 } 44 int t1=0,temp,t2=0; 45 int max1=-1; 46 for(int i=1; i<n; i++) 47 { 48 memset(c,0,sizeof(c)); 49 for(int j=i+1; j<=n; j++) 50 { 51 if(a[i]>a[j]) 52 { 53 t1++; 54 temp=1+2*sum(a[j]); 55 if(temp>max1) {max1=temp; t2=0;} 56 if(temp==max1) t2++; 57 add(a[j]); 58 } 59 } 60 } 61 printf("%d %d\n",t1-max1,t2); 62 } 63 return 0; 64 }