Xidianoj 1024 Simple Reverse

Source: Internet
Author: User

Title Description reverse order to the problem for everyone is already very familiar with the problem, that is, when seeking I<j, A[i] > A[j] The number of groups. Now, please find out the number of reverse pairs in a series of numbers, it should be noted that these numbers are within [0,9].

Enter the first line input T, indicating that there is a T group of test data for each group of data, first enter N, representing the number of N (0<n<=10^6) Next enter n number, each number is within [0,9]

Output

Output the number of reverse pairs, and the 10^9+7 modulo

--Text

Using the merge sort to reverse the order, in fact there are many kinds of

PS: Playing 10^9 time is always careless more dozen a 0 less dozen a 0,wa a half a day, eh

#include <stdio.h>inta[1000001];inttempa[1000001];Long LongTotal =0;voidMergeintLeftintMidintRight ) {    intEndl = mid-1, Endr =Right ; intL = Left,r =mid; intTMP =Left ; intnumelements = Right-left +1;  while(l<= Endl && R <=Endr) {        if(A[l] <=A[r]) {tempa[tmp+ +] = a[l++]; }        Else{ Total+ = Endl-l +1; Tempa[tmp+ +] = a[r++]; }    }      while(l<=Endl) {tempa[tmp+ +] = a[l++]; }     while(r<=Endr) {tempa[tmp+ +] = a[r++]; }    inti;  for(i=0; i<=numelements;i++,endr--) {A[endr]=Tempa[endr]; }}voidMergeSort (intLeftintRight ) {    intMid = (left + right)/2; if(Left <Right )        {mergesort (Left,mid); MergeSort (Mid+1, right); Merge (Left,mid+1, right); }} intMain () {inttime,t; scanf ("%d",&T);  for(time=1; time<=t;time++){        intN; scanf ("%d",&N); inti;  for(i=1; i<=n;i++) {scanf ("%d",&A[i]); } Total=0; MergeSort (1, N); printf ("%lld\n", Total%1000000007); }        return 0;}

Xidianoj 1024 Simple Reverse

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.