35 reverse order in the array

Source: Internet
Author: User

Title Description

In the array of two digits, if the previous number is greater than the number that follows, then these two numbers form an inverse pair. Enter an array to find the total number of reverse pairs in this array p. and the output of p to 1000000007 modulo is obtained. i.e. Output p%1000000007

Input Description:
The title guarantees that the input array does not have the same number
Data range:
For%50 data, size<=10^4
For%75 data, size<=10^5
For%100 data, size<=2*10^5

Example 1

Lose
1,2,3,4,5,6,7,0

Output

7

Ideas:
Similar to Megersort

Counting the number of inverse pairs inside the subarray, and then counting the number of reverse pairs between the 2 adjacent sub-arrays, in the process of statistics, we also need to sort the array,
Sorting can avoid repeating statistics.

1  Public classSolution {2      Public intInversepairs (int[] a) {3         int[] aux =New int[a.length];4          for(inti = 0;i<a.length;i++)5Aux[i] =A[i];6         7         returnFun (a,aux,0,a.length-1);8     }9     Private intFunint[] A,int[] aux,intLointhi) {Ten         if(Lo==hi)return0; One         intMid = (Hi-lo)/2+Lo; A         intleft = Fun (A,aux,lo,mid)%1000000007; -         intright = Fun (A,aux,mid+1,hi)%1000000007; -          the         inti =mid; -         intj =Hi; -         intai = Hi;//Aux_index -         intCount = 0; +          while(i>=lo&&j>=mid+1){ -             if(a[i]>A[j]) { +aux[ai--] = a[i--]; Acount+=j-mid; at                 if(count>=1000000007)//The value is too large to seek redundancy -count%=1000000007;  -             } -             Else -aux[ai--]=a[j--]; -         } in          -          while(i>=lo) toaux[ai--]=a[i--]; +          while(j>=mid+1) -aux[ai--] =a[j--];  the          *         //updating an array $          for(intK = lo;k<=hi;k++)Panax NotoginsengA[K] =Aux[k]; -          the         return(Left+right+count)%1000000007; +     } A}

35 reverse order in the array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.