The reverse order in the array

Source: Internet
Author: User

The title describes the two numbers in the array, and if the previous number is greater than the number that follows, the 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



Input Example:
1,2,3,4,5,6,7,0

Output Example:
7


Analysis: The general idea of the problem is the complexity of the square of N, each number in turn and the following all the comparison, if the front is greater than the back, count++. But this result is certainly not the best answer, the best solution is n*log n complexity of the solution.
Similar to the method of merging sorting, the array is divided into two parts, if the two parts have been arranged in order (from small to large), and has been calculated, the reverse of the two parts of the inverse pair, then this time the inverse of this array is only possible the first half of the
A second half of a, because in the same part of the already found out (you forget what to look for, assuming that the finished), this time to compare the front and back of the two parts to see, two pointers, from the forward to move forward from the front of the pointer number is greater than the back, then the front part of the
All the numbers that follow the current pointer are larger than the number that the current pointer points to in the following section. Keep the smaller copy in a cache array and move the small pointer back. The termination condition is that the array length is 1.

On the Code
 Public classSolution { Public intInversepairs (int[] Array) {        if(array==NULL|| Array.length==0){            return0; }        int[] copy=New int[Array.Length]; intAns=get (array,copy,0,array.length-1); returnans; }     Public intGetint[] Array,int[] Copy,intStartintend) {        if(start==end) {            return0; }        intMid= (start+end)/2; intLeftcount=get (Array,copy,start,mid)%1000000007 ; intRightcount=get (array,copy,mid+1,end)%1000000007 ; intFir=start; intSec=mid+1; intI=0; intCount=0;  while(fir<=mid&&sec<=end) {            if(array[fir]>Array[sec]) {Copy[i++]=Array[sec]; SEC++; Count+ = (mid-fir+1); if(count>1000000007) {Count%=1000000007; }                            }Else{copy[i++]=Array[fir]; Fir++; }                    }         while(fir<=mid) {Copy[i++]=Array[fir]; Fir++; }         while(sec<=end) {Copy[i++]=Array[sec]; SEC++; }         for(intk=0;k<=end-start;k++) {array[k+start]=Copy[k]; }        return(Count+leftcount+rightcount)%1000000007 ; }}

The copy array, which is always passed into the index instead of being created in the recursive function to prevent timeouts.





The 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.