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
6 0 "Analysis"
Start discretization with map T for half a day, not alive.
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <vector>6#include <utility>7#include <iomanip>8#include <string>9#include <cmath>Ten#include <map> One A Const intMAXN =500000+Ten; - Const intMAXM =500000+Ten; - //const int MAXM = n + ten; the Const intMAXL =Ten; - using namespacestd; - structdata{ - intVal; + intorder; - BOOL operator< (DATA b)Const{ + returnVal <B.val; A } at }REM[MAXN]; -typedefLong Longll; - intN; - intDATA[MAXN]; - intC[MAXN]; - in voidinit () { - for(inti =1; I <= N; i++) { toscanf"%d", &data[i]); +C[i] =0; -Rem[i].val =Data[i]; theRem[i].order =i; * } $Sort (REM +1, REM +1+n);Panax Notoginseng for(inti =1; I <= N; i++) Data[rem[i].order] =i; - //for (int i = 1; I <= n; i++) printf ("%d\n", Data[i]);p rintf ("\ n"); the } + intLowbit (intx) {returnx&-x;} A intSumintx) { the intCNT =0; + while(X >0){ -CNT + =C[x]; $X-=lowbit (x); $ } - returnCNT; - } the voidAddintx) { - while(x <=N) {Wuyic[x]++; theX + =lowbit (x); - } Wu return; - } About $ voidWork () { -ll Ans =0; - //The front i-1 a number - for(inti =1; I <= N; i++){ AAns + = (I-1-SUM (data[i]);//strictly greater than + Add (Data[i]); the } -printf"%lld\n", Ans); $ } the the intMain () { the #ifdef LOCAL theFreopen ("Data.txt","R", stdin); -Freopen ("OUT.txt","W", stdout); in #endif the while(SCANF ("%d", &n) &&N) { the init (); About Work (); the } the return 0; the } +
View Code
"POJ2266" "Tree array + discretization" Ultra-quicksort