POJ 2299 Ultra-quicksort

Source: Internet
Author: User

Ultra-quicksort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 53630 Accepted: 19693

Description

In this problem, you has to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping, adjacent sequence elements until the sequence is Sorted in ascending order. For the input sequence
9 1 0 5 4,
Ultra-quicksort produces the output
0 1 4 5 9.
Your task is to determine what many swap operations Ultra-quicksort needs to perform in order to sort a given input sequenc E.

Input

The input contains several test cases. Every test case begins with a line this contains a single integer n < 500,000-the length of the input sequence. Each of the following n lines contains a single integer 0≤a[i]≤999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must is processed.

Output

For every input sequence, your program prints a single line containing an integer number OP, the minimum number of swap op Erations necessary to sort the given input sequence.

Sample Input

59105431230

Sample Output

60

Source

Waterloo Local 2005.02.05

[Submit]   [Go back]   [Status] [Discuss]

Home Page Go back to top

—————————————————— I'm a split line ————————————————————————

Good question. Merge sort to find reverse order.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <queue>7#include <cstdlib>8#include <iomanip>9#include <cassert>Ten#include <climits> One #defineMAXN 10001 A #defineF (i,j,k) for (int i=j;i<=k;i++) - #defineFF (i,j,k) for (int i=j;i>=k;i--) - #defineINF 0x7fffffff the #defineNN 500004 - #defineNL 1000 - #defineMem (a) memset (a, 0, sizeof (a)) - using namespacestd; + intN, a[500010], t[500010]; - __int64 ans; + intRead () { A     intx=0, f=1;CharCh=GetChar (); at      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} -     returnx*F; - } - __int64 Res; - intB[nn]; in voidCopyintA[],intLintR) { -     inti; to      for(i = l; I <= R; i++){ +A[i] =B[i]; -     } the } * voidMergeintA[],intLintMidintR) { $     inti =l;Panax Notoginseng     intj = Mid +1; -     intK =l; the      while(I <= mid && J <=R) { +         if(A[i] <A[j]) { Ab[k++] =A[i]; thei++; +}Else{ -b[k++] =A[j]; $J + +; $Res + = Mid-i +1; -         } -     } the      while(I <=mid) { -b[k++] =A[i];Wuyii++; the     } -      while(J <=R) { Wub[k++] =A[j]; -J + +; About     } $ } - voidMergeSort (intA[],intLintR) { -     if(L <R) { -         intMid = (L + r) >>1; A MergeSort (A, L, mid); +MergeSort (A, Mid +1, R); the merge (A, L, Mid, R); - copy (A, L, R); $     } the } the intMain () { the     intn, I; the     intF[nn]; -      while(cin>>n&&N) { in         if(n = =0) Break; the          for(i =1; I <= N; i++){ theCin>>F[i]; About         } theres =0; theMergeSort (F,1, n); thecout<<res<<Endl; +     } -     return 0; the}
POJ 2299

POJ 2299 Ultra-quicksort

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.