标签:put closed one col span lap output logs str
第1行:N,N为序列的长度(n <= 50000) 第2 - N + 1行:序列中的元素(0 <= A[i] <= 10^9)
输出逆序数
4 2 4 3 1
4
看程序吧 不会讲
1 #include <iostream> 2 using namespace std; 3 #include<string.h> 4 #include<set> 5 #include<stdio.h> 6 #include<math.h> 7 #include<queue> 8 #include<map> 9 #include<algorithm> 10 #include<cstdio> 11 #include<cmath> 12 #include<cstring> 13 #include <cstdio> 14 #include <cstdlib> 15 #include<stack> 16 #include<vector> 17 int a[51000]; 18 int b[51000]; 19 long long sum=0; 20 void dfs1(int kaishi,int zhongjian,int jieshu) 21 { 22 int i=kaishi,j=zhongjian+1; 23 int k=kaishi; 24 while(i<=zhongjian&&j<=jieshu) 25 { 26 if(a[i]<a[j]) 27 b[k++]=a[i++]; 28 else 29 { 30 31 b[k++]=a[j++]; 32 sum+=zhongjian-i+1; 33 //cout<<j<<"_"<<k<<endl; 34 continue; 35 } 36 } 37 while(i<=zhongjian) 38 { 39 b[k++]=a[i++]; 40 } 41 while(j<=jieshu) 42 { 43 b[k++]=a[j++]; 44 } 45 for(i=kaishi;i<=jieshu;i++) 46 a[i]=b[i]; 47 } 48 void dfs(int kaishi,int jieshu) 49 { 50 if(kaishi<jieshu) 51 { 52 int zhongjian=(kaishi+jieshu)/2; 53 dfs(kaishi,zhongjian); 54 dfs(zhongjian+1,jieshu); 55 dfs1(kaishi,zhongjian,jieshu); 56 } 57 } 58 int main() 59 { 60 int n; 61 scanf("%d",&n); 62 for(int i=0;i<n;i++) 63 { 64 scanf("%d",&a[i]); 65 b[i]=a[i]; 66 } 67 dfs(0,n-1); 68 cout<<sum<<endl; 69 return 0; 70 }
标签:put closed one col span lap output logs str
原文地址:http://www.cnblogs.com/dulute/p/7499093.html