Seeking inverse number (C language) - merge

. A: Numbers by reverse order
described
in one arrangement, if the longitudinal position of a pair of numbers in reverse order of magnitude, i.e. the number greater than the number in front of the latter, they are called a reverse order. A reverse arrangement on the total number of the called number in reverse order.

Now, to give you a sequence of N elements, you determine the number of reverse it is.

For example inverse number of 1132 it is.

Format
input format
number represents a set of first rows of test data input integer T (1 <= T <= 5)
for each data line of each test is an integer N represents the number of columns in a total of N elements (2 <= N <= 100000)
followed by a row of a total of N integers Ai (0 <= Ai <1000000000 ), it represents the number of all elements in the column.

A plurality of sets of data to ensure that the test data, the test data of more than 100,000 at most a number of groups.

Output format of
the output of the number of columns the number of reverse

Sample
Sample input the Copy
2
2
. 1. 1
. 3
. 1. 3 2
sample output the Copy
0
. 1

Analysis: This question can ask how many numbers with merge sort in reverse order, in fact, at the same time merging of the event in reverse order on the use of mid-x 1 + like a record, and merge sort is a template, remember to well
Code:

#include <stdio.h>
#include <string.h>
#include <math.h>
long long res[100000]={0},count,a[100000]={0}; //a数组存放原来数据,res数组存放每次msor排好序的结果,count记录逆序数
long long msort(long long *a,long long left,long long mid,long long right)
{
    long long x=left,y=mid+1,k=left;
    while(x<=mid&&y<=right)
    {
        if(a[x]<=a[y]) //这里把正序的数一次放进数组
        {
            res[k++]=a[x++];
        }
        else
        {
            count+=mid-x+1; //如果反序的话则记录起来,因为反序的话后面的也要算起来,所以mid-x+1,如果不求逆序数的话这条可省掉
            res[k++]=a[y++];
        }
    }
        while(x<=mid) res[k++]=a[x++];
        while(y<=right) res[k++]=a[y++]; //检测漏网的数
        for(x=left;x<=right;x++)
        {
            a[x]=res[x];
        } //把每次排好的序放进原数组
}
long long merge(long long *a,long long left,long long right)
{
    if(left>=right) return ;
    long long mid;
    mid=(left+right)/2;
    merge(a,left,mid); //递归排左边数的数
    merge(a,mid+1,right); //递归排右边的序
    msort(a,left,mid,right); //每轮递归结束后的总理
}
int main()
{
    long long t,n,x;
    while(scanf("%lld",&t)!=EOF)
    {
        while(t--)
        {
            scanf("%lld",&n);
            for(x=1;x<=n;x++)
                scanf("%lld",&a[x]);
            count=0;
            merge(a,1,n);
            printf("%lld\n",count);
        }
    }
    return 0;
}

Merge to understand the words on the operation of recursion have to be clear, it simply is the main function to mobilize, merge function is divided regions around slowly separate merge, then msort function ordering, left when recursive, right, mid is very important to pass parameters , not only the sort area, divided areas of the traditional values ​​are drained sequence

发布了3 篇原创文章 · 获赞 0 · 访问量 1491

Guess you like

Origin blog.csdn.net/qq_46182442/article/details/104055951