Ultra-quicksort (tree-like array + discretization)

Source: Internet
Author: User

ultra-quicksort POJ 2299
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 50495 Accepted: 18525

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 on order to sort a given input seq Uence.

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 SWA P operations necessary to sort the given input sequence.

Sample Input

59105431230

Sample Output

60
Tree-like array maintenance: C[i] Stores a smaller number than I.
Discretization: For example, input 9 1 0 5 4, convert it to 5 2 1 4 3 for easy storage.
Refer to someone else's code, read the long time!
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5 #defineLL Long Long6 #defineMax 500000+107 using namespacestd;8 structnode9 {Ten     intNum,order; One }; A intN; - intC[max];  -Nodeinch[Max]; the intT[max]; - intCMP (node A,node b) - { -     returna.num<B.num; + } - voidAddintIinta) + { A      while(i<=N) at     { -t[i]+=A; -i+=i&-i; -     } - } - intSuminti) in { -     ints=0; to      while(i>=1) +     { -s+=T[i]; thei-=i&-i; *     } $     returns;Panax Notoginseng } - intMain () the { +     inti,j; AFreopen ("In.txt","R", stdin); the      while(~SCANF ("%d", &n) &&N) +     { -memset (T,0,sizeof(t)); $          for(i=1; i<=n;i++) $         { -scanf"%d",&inch[I].num]; -             inch[i].order=i; the         } -Sortinch+1,inch+1+n,cmp);Wuyi          for(i=1; i<=n;i++)/*discretization of*/ thec[inch[i].order]=i; -LL s=0; Wu          for(i=1; i<=n;i++) -         { AboutAdd (C[i],1); $s+=i-sum (c[i]); -     //cout<<s<<endl; -              -         } Aprintf"%lld\n", s); +     } the}

Ultra-quicksort (tree-like array + discretization)

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.