1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #include <list> 9 #include <ctime> 10 #include <set> 11 #include <cstring> 12 #include <queue> 13 #include <cstdio> 14 #include <stack> 15 using namespace std; 16 17 int main() { 18 vector <int> res; 19 int n,t; 20 cin>>n; 21 while (n--) { 22 cin>>t; 23 res.push_back(t); 24 } 25 sort(res.begin(),res.end()); 26 cout<<res[res.size()/2]; 27 return 0; 28 }
merge排序竟然能用来求逆序数,长见识了....
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #include <list> 9 #include <ctime> 10 #include <set> 11 #include <cstring> 12 #include <queue> 13 #include <cstdio> 14 #include <stack> 15 using namespace std; 16 const int N=500001; 17 int a[N]; 18 int c[N]; 19 long long cnt; 20 void MergeSort(int l, int r){ 21 int mid, i, j, tmp; 22 if( r > l+1 ){ 23 mid = (l+r)/2; 24 MergeSort(l, mid); 25 MergeSort(mid, r); 26 tmp = l; 27 for( i=l, j=mid; i < mid && j < r; ){ 28 if( a[i] > a[j] ){ 29 c[tmp++] = a[j++]; 30 cnt += mid-i; 31 } 32 else c[tmp++] = a[i++]; 33 } 34 if( j < r ) for( ; j < r; ++j ) c[tmp++] = a[j]; 35 else for( ; i < mid; ++i ) c[tmp++] = a[i]; 36 for ( i=l; i < r; ++i ) a[i] = c[i]; 37 } 38 } 39 int main() { 40 int n; 41 scanf("%d",&n); 42 while (n) { 43 cnt=0; 44 for(int i=0;i<n;i++){ 45 scanf("%d",&a[i]); 46 } 47 MergeSort(0,n); 48 cout<<cnt<<endl; 49 scanf("%d",&n); 50 } 51 return 0; 52 }