[Hiho] binary and merge sort in reverse order

Source: Internet
Author: User

Describe

We knew Nettle was playing "Battleship これ" on the previous, last, and upper back. After a bitter struggle, nettle again got a lot of boats.
This day nettle is checking its fleet list:

As we can see, the ship's default sort is the rank parameter. But in fact a ship's fire value and the level of the relationship is not big, so there will be a ship than B ship grade, but a ship fire is lower than the case of B ship. For example, the 77-stage dragon to two firepower is less than the 55-level of the XI-Li two.
Now that the nettle will give all the ship's firepower in order of rank, please calculate how many pairs of ships meet the above mentioned situation.

Tip: High firepower is fierce!

Input

Line 1th: an integer n. N indicates the number of ships, 1≤n≤100,000
Line 2nd: n integers, the number of I indicates the fire value of the ship with low rank a[i],1≤a[i]≤2^31-1.

Output

Line 1th: An integer that indicates how many pairs of ships meet "a ship is higher than B ship, but a ship's firepower is lower than ship B."

Sample input

101559614248 709366232 500801802 128741032 1669935692 1993231896 369000208 381379206 962247088 237855491

Sample output

27

Problem Solving Ideas: The merge sort is to divide the series A[l,h] into two halves a[l,mid] and a[mid+1,h], respectively, and then combine the two halves together. In the process of merging (set L<=i<=mid,mid+1<=j<=h), when the a[i]<=a[j], does not produce reverse order number, when A[i]>a[j], in the first half than the a[i] larger than the number of a[j], will a[j] Put in front of a[i], reverse order number to add mid+1-i. Therefore, you can calculate the number of reverse orders during the merge process in a merged sort.
Note the range, the maximum number of reverse order is n (n-1)/2, the result is saved with a long long.
#include <stdio.h>using namespacestd;Const intN =100000+5;intA[n],tmp[n];Long Longans;voidMerge (intLintMintR) {    inti =l; intj = m +1; intK =l;  while(I <= m && J <=r) {if(A[i] >A[j]) {tmp[k+ +] = a[j++]; Ans+ = M-i +1; }        Else{tmp[k+ +] = a[i++]; }    }     while(i <= m) tmp[k++] = a[i++];  while(J <= r) tmp[k++] = a[j++];  for(inti=l;i<=r;i++) A[i]=tmp[i];}voidMerge_sort (intLintR) {    if(L <r) {intm = (L + r) >>1;        Merge_sort (L,M); Merge_sort (M+1, R);    Merge (L,M,R); }}intMain () {intN; //freopen ("In.txt", "R", stdin);     while(SCANF ("%d", &n)! =EOF) {         for(intI=0; i<n;i++) scanf ("%d",&A[i]); Ans=0; Merge_sort (0, N-1); printf ("%lld\n", ans); }    return 0;}

[Hiho] binary and merge sort in reverse order

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.