老方法,看注释
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int num1,num2;
void qsort(int a[],int l,int r){
if(l>=r)
return ;
int i = l;
int j = r;
int k = a[i];
while(i<j){
while(i<j && a[j]>=k)j--;
if(a[i]!=a[j])num1++;
a[i] = a[j];
while(i<j && a[i]<=k)i++;
if(a[i]!=a[j])num1++;
a[j] = a[i];
}
a[i] = k;
qsort(a,l,i-1);
qsort(a,i+1,r);
}
int main()
{
int n;
int a[100010];
int b[100010];
while(cin>>n){
for(int i=0; i<n; i++){
cin>>a[i];
b[i] = a[i];
}
num1 = 0;
qsort(b,0,n-1);
num2 = 0;
for(int i=0; i<=n-2; i++){
for(int j=0; j<=n-2-i;j++){
if(a[j]>a[j+1]){
num2++;
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
cout<<num2<<" "<<num1<<endl;
}
return 0;
}