比较魔幻的实现思路,核心思想为,val%(max+1)为原数值,val/(max+1)为未来的数值
要求1:为正数,负数也行,加个abs,但是unsigned就不能用了
要求2:使用unsigned long long 因为,max*(max+1)会超过int上限
C++ 代码
unsigned long long li[100001];
void merge_sort(unsigned long long q[],int l,int r){
if(l>=r)return;
int mid = l+r>>1;
merge_sort(q,l,mid);merge_sort(q,mid+1,r);
int max = q[l];
for(int i=l;i<=r;i++)max = q[i]>max?q[i]:max;
int i = l,j = mid+1,idx = l;
while(i<=mid && j<=r)
if(q[i]%(max+1)<=q[j]%(max+1)) q[idx++] += (q[i++]%(max+1))*(max+1);
else q[idx++] += (q[j++]%(max+1))*(max+1);
while(i<=mid) q[idx++] += (q[i++]%(max+1))*(max+1);
while(j<=r) q[idx++] += (q[j++]%(max+1))*(max+1);
for(int i=l;i<=r;i++)q[i]/=(max+1);
}