Merge sort find reverse order number

Source: Internet
Author: User

Test Instructions:Bobo has a sequence a1,a 2,..., a n. He is allowed to swap, adjacent numbers for no more than k times.
Find the minimum number of inversions after his swaps.
Note:the number of inversions is the number of pair (I,J) where 1≤i<j≤n and a i>a J.

Input

The input consists of several tests. For each tests:
The first line contains 2 integers n,k (1≤n≤10 5,0≤k≤10 9). The second line contains n integers a 1,a 2,..., a n (0≤a i≤10 9).

Output

For each tests:
A single integer denotes the minimum number of inversions.

Sample Input

3 12 2 13 02 2 1

Sample Output

12 Analysis: Merge sort + greedy Here's a k to note, that is, you can exchange the location of adjacent numbers, so that the number of reverse order is the smallest, then we must be the smallest number forward, so that we calculate the number of reverse order as long as the original number of reverse order and then subtract the number of times can be exchanged is good, Of course, if we figure out a negative number, let it be directly equal to zero! Code:
1#include <iostream>2 using namespacestd;3 Const intMAXN =100000+5;4 Const intQ =50000+5;5 intARR[MAXN];6 intLef[q], rig[q];7 Long Longans;8 intlef_size;9 intrig_size;Ten voidMerger (intArr[],intLintMintR) One{//Merge entire array A     intI, J, K; -Lef_size = M-L; -Rig_size = R-M; the      for(i = L; i < M; i++) -     { -LEF[I-L] = Arr[i];//the array on the left -     } +      for(i = M; i < R; i++) -     { +RIG[I-M] = Arr[i];//the array on the right A     } ati =0; j =0; K =L; -      while(I < Lef_size&&j <rig_size) -     { -         if(Lef[i] <= rig[j])//The left array is less than the right array -         {  -ARR[K] = Lef[i];//Put the number of the left array into the consolidated array ini++;//left array subscript gaga -k++;//Integrated Array subscript + + toAns + = j;//Reverse number Gaga (equivalent to looking for the number I inserted between I and the next I, as long as there is a number in the queue, it means +                                                         //The left (or the front) is greater than the number on the right (back), so adding J may be a bit difficult to understand, -                                                            //self-brain repair it! ) the         } *         Else $         {Panax NotoginsengARR[K] =Rig[j]; -J + +; thek++; +         } A     } the      while(I <lef_size) +{//the number of left that has not been put out -  $ARR[K] =Lef[i]; $i++; -k++; -Ans + =J; the  - Wuyi     } the      while(J <rig_size) -     { WuARR[K] =Rig[j]; -J + +; Aboutk++; $  -     } - } - voidMergersort (intArr[],intLintR) A { +     if(L +1&LT;R)//Merge Sort the     { -         intM = (L + R)/2; $ Mergersort (arr, L, M);  the Mergersort (arr, M, R); the merger (arr, L, M, R); the     } the } - intMain () in { the     intN; the     Long LongK; About      while(Cin >> N>>k) the     { theAns =0; the          for(intm =0; M < n; m++) +CIN >>Arr[m]; -Mergersort (arr,0, n); the         if(Ans-k >0)Bayicout << ans-k <<Endl;  the         Else thecout <<"0"<<Endl; -     } -     return 0; the}

Merge sort find reverse order number

Related Article

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.