数组小和的定义如下:
例如,数组s=[1,3,5,2,4,6],在s[0]的左边小于或等于s[0]的数的和为0,在s[1]的左边小于或等于s[1]的数的和为1,在s[2]的左边小于或等于s[2]的数的和为1+3=4,在s[3]的左边小于或等于s[3]的数的和为1,在s[4]的左边小于或等于s[4]的数的和为1+3+2=6,在s[5]的左边小于或等于s[5]的数的和为1+3+5+2+4=15,所以s的小和为0+1+4+1+6+15=27。
给定一个数组s,实现函数返回s的小和。
public class SmallSum { public static void main(String[] args) { int [] arr = new int[] {1,3,5,2,4,6}; int smallSum = sort(arr, 0 , arr.length-1); System.out.println(smallSum); } public static int sort(int[] arr , int L , int R) { if(L == R) { return 0; } int mid = L+((R-L)>>1); return sort(arr,L,mid)+sort(arr,mid+1,R)+merge(arr,L,mid,R); } private static int merge(int [] arr, int L, int mid, int R) { int [] help = new int[R-L+1]; int i = 0; int p1 = L; int p2 = mid+1; int res = 0; while(p1<=mid && p2<= R) { res += arr[p1] <= arr[p2] ? arr[p1] * (R-p2+1):0; help[i++] = arr[p1] <= arr[p2] ? arr[p1++]:arr[p2++]; } while(p1<=mid) { help[i++] = arr[p1++]; } while(p2<=R) { help[i++] = arr[p2++]; } for(i= 0 ; i<help.length; i++ ) { arr[L+i] = help[i]; } return res; } }