有意思的是蓝桥杯的这道题目要求的是每个单位被逆序队包含的次数
#include<iostream> #include<cstdio> #include<algorithm> #define maxn 1e5+1 using namespace std; int time[maxn]; void mer(int a[],int low,int mid,int hight) { int i=low,j=mid+1; while(i<=mid&&j<=hight) { if(a[i]<=[j]) { i++; } else { time[a[j]]+=mid-i+1; j++; } } int i=mid,j=hight; while(i>=low&&j>=mid+1) { if(a[j]>=a[i]) { j--; } else { time[a[i]]+=hight-j+1; i--; } }//这里正反两次求逆序 比较快的算出一个数被逆序对包含的次数! sort(a+low,a+hight+1,cmp); } void mergesort(int a[],int low,int hight) { int mid=(low+high)/2; if(low<hight) { mergesort(a,low,mid);//左边有序 mergesort(a,mid+1,hight);//右边有序 mer(a,low,mid,hight);//归并 } } int main() { return 0; }