[Hihocoder] binary and merge sort in reverse order

Source: Internet
Author: User

Time limit:10000msSingle Point time limit:1000msMemory Limit:256MB
description

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

Import Java.util.arrays;import Java.util.scanner;public class Main {long res = 0;private void sort (int start,int end,int[] ARR) {if (start>=end) return;int mid = (start+end)/2;sort (Start,mid,arr); sort (mid+1,end,arr); merge (Start,end,arr) ;} private void merge (int start,int end,int[]arr) {int mid = (start+end)/2;int insert[] = new Int[mid-start+1];insert = Arrays . Copyofrange (arr, start, mid+1); int idx = Start;int left = 0;int right = Mid+1;while (Left<=insert.length-1&&ri Ght<=end) {if (Arr[right]<insert[left]) {arr[idx++] = arr[right++];} Else{res + = right-(mid+1); arr[idx++]=insert[left++];}} while (left<=insert.length-1) {arr[idx++] = insert[left++];res+= right-(mid+1);}} public static void Main (String args[]) {/*scanner sc = null;try {sc = new Scanner (New FileInputStream ("D://desktop//test.tx T "));} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} */scanner sc = new Scanner (system.in); int num = Sc.nextint (); int []arr = new Int[num];for (int i=0;i<num;i++) {Arr[i] = Sc.nextint ();} Main main = new main (); main.res = 0;main.sort (0,arr.length-1,arr); System.out.println (Main.res);}}


[Hihocoder] 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.