#include<iostream>
#include<fstream>
#define rep(i, n) for(i=0;i<n;i++)
#define repi(i,l,n) for(i=l;i<=r;i++)
using namespace std;
void quicksort(int l, int r);
int a[100];
int b[100];
int hit;
int main(){
int i, k;
cin >> k;
rep(i, k) cin >> a[i];
quicksort(0, k - 1);
rep(i, k) cout << a[i];
cout << hit << endl;
getchar(); getchar(); //crash
}
void quicksort(int l, int r){
if (l < r){
int round = l + rand() % (r - l + 1);
int set = a[round];
int i, al = 0, br = 0, il = l, ir, count = 0;
repi(i, l, r){
if (a[i]<set) count++;
}
b[count + l] = set;
ir = count + 1+l;
repi(i, l, r){
if (a[i] > set)
{
b[ir++] = a[i];
al++;
if (i < round)
hit++;
}
else if (a[i] < set)
{
b[il++] = a[i];
br++;
if (i>round)
hit++;
hit += al;
}
}
repi(i, l, r){
a[i] = b[i];
}
quicksort(l, count);
quicksort(count + 1, r);
}
}
用快排思路解决逆序对问题
最新推荐文章于 2024-01-26 18:20:48 发布