poj2299 Ultra-quicksort tree-like array for reverse order number

Source: Internet
Author: User

poj2299 Ultra-quicksort tree-like array for inverse number Ultra-quicksort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 49587 Accepted: 18153

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

59 1 0) 5 4
3
1 2 30

Sample Output

60
The first tree-like array, the number of reverse order is actually the number of discretization and then inserted in the past after inserting a tree array or line segment tree, and statistics on the front of the number of larger than it, sum can be.
But the tree-like array is very simple to write ....
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intInf= (1<< in);intn;ll a[maxn];ll b[maxn],m;ll C[MAXN];intLowbit (intx) {    returnx& (-x);} ll sum (intp) {LL res=0;  while(p>0) {res+=C[p]; P-=lowbit (P); }    returnRes;}voidAddintp,ll x) {     while(p<=m) {C[p]+=x; P+=lowbit (P); }}intMain () { while(cin>>n,n)        {MS0 (c); REP (i,1, N) {scanf ("%i64d",&A[i]); B[i]=A[i]; } sort (b+1, b+n+1); M=unique (b +1, b+n+1)-(b +1); ll ans=0; REP (i,1, N) {            intX=lower_bound (b +1, b+n+1, A[i])-b; Add (x,1); Ans+=sum (M)-sum (x); } printf ("%i64d\n", ans); }    return 0;}
View Code

poj2299 Ultra-quicksort tree-like array for reverse order number

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.