高盛 一个三次方优化成二次方的问题:先计数,然后用smaller * bigger

https://www.geeksforgeeks.org/count-inversions-array-set-3-using-bit/

 

数数完了之后把count先存一下,挺好

 

// A O(n^2) Java program to count inversions of size 3

class Inversion {
    
    // returns count of inversion of size 3
    int getInvCount(int arr[], int n)
    {
        int invcount = 0; // initialize result
        
        for (int i=0 ; i< n-1; i++)
        {
            // count all smaller elements on right of arr[i]
            int small=0;
            for (int j=i+1; j<n; j++)
                if (arr[i] > arr[j])
                    small++;
                    
            // count all greater elements on left of arr[i]
            int great = 0;
            for (int j=i-1; j>=0; j--)
                        if (arr[i] < arr[j])
                            great++;
                    
            // update inversion count by adding all inversions
            // that have arr[i] as middle of three elements
            invcount += great*small;
        }
        return invcount;
    }
    // driver program to test above function
    public static void main(String args[])
    {
        Inversion inversion = new Inversion();
        int arr[] = new int[] {8, 4, 2, 1};
        int n = arr.length;
        System.out.print("Inversion count : " +
                    inversion.getInvCount(arr, n));
    }
}

// This code has been contributed by Mayank Jaiswal

 

posted @ 2021-10-07 08:34  苗妙苗  阅读(74)  评论(0编辑  收藏  举报